diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 64faa3f..d7343ff 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -52,6 +52,11 @@ jobs: - name: README command tests shell: bash run: bash tests/readme-commands.sh + # Exercise the end-user launcher (download-if-missing + serve + teardown) + # against the dummy fixtures and a local http.server standing in for HF. + - name: Launcher smoke test + shell: bash + run: bash tests/launcher-smoke.sh - name: Package if: startsWith(github.ref, 'refs/tags/v') shell: bash @@ -62,6 +67,8 @@ jobs: cp -r default_voices emotion_directions dist/ mkdir -p dist/scripts cp scripts/normalize-text.py dist/scripts/ + cp scripts/start-zonos2.sh dist/start-zonos2.sh + chmod +x dist/start-zonos2.sh tar -C dist -czf zonos2-linux-x64-cpu.tar.gz . - uses: actions/upload-artifact@v4 if: startsWith(github.ref, 'refs/tags/v') @@ -138,6 +145,10 @@ jobs: cp -r default_voices emotion_directions dist/ mkdir -p dist/scripts cp scripts/normalize-text.py dist/scripts/ + # stamp the launcher's backend default to match this archive's build + sed 's/^GPU_DEFAULT=cpu/GPU_DEFAULT=gpu/' scripts/start-zonos2.sh > dist/start-zonos2.sh + grep -q '^GPU_DEFAULT=gpu' dist/start-zonos2.sh || { echo "GPU stamp failed"; exit 1; } + chmod +x dist/start-zonos2.sh tar -C dist -czf zonos2-linux-x64-vulkan.tar.gz . - uses: actions/upload-artifact@v4 if: startsWith(github.ref, 'refs/tags/v') @@ -145,10 +156,71 @@ jobs: name: zonos2-linux-x64-vulkan path: zonos2-linux-x64-vulkan.tar.gz + # -------------------------------------------------------- Linux desktop app + # zonos2-app (saucer webview shell). Needs ubuntu-24.04: saucer hard-requires + # GTK4 >= 4.12 + a C++23 compiler (gcc-14), both beyond the 22.04 jobs above, + # which stay on 22.04 to keep the glibc baseline of the CLI artifacts. + linux-app: + runs-on: ubuntu-24.04 + steps: + - uses: actions/checkout@v4 + with: + submodules: recursive + - name: Install desktop + Vulkan deps + run: | + wget -qO - https://packages.lunarg.com/lunarg-signing-key-pub.asc | sudo tee /etc/apt/trusted.gpg.d/lunarg.asc >/dev/null + sudo wget -qO /etc/apt/sources.list.d/lunarg-vulkan-noble.list https://packages.lunarg.com/vulkan/lunarg-vulkan-noble.list + sudo apt-get update + sudo apt-get install -y vulkan-sdk gcc-14 g++-14 \ + libgtk-4-dev libwebkitgtk-6.0-dev libadwaita-1-dev libjson-glib-dev + - name: Resolve ggml submodule rev + id: ggml + run: echo "sha=$(git rev-parse HEAD:ggml)" >> "$GITHUB_OUTPUT" + - uses: hendrikmuhs/ccache-action@v1.2 + with: + key: linux-app + # Same shader-gen build-tree cache rationale as the linux-vulkan job. + - name: Cache build tree + uses: actions/cache@v4 + with: + path: build + key: linux-app-build-${{ steps.ggml.outputs.sha }}-${{ hashFiles('src/**', 'CMakeLists.txt') }} + restore-keys: | + linux-app-build-${{ steps.ggml.outputs.sha }}- + - name: Configure + run: > + cmake -B build -DCMAKE_BUILD_TYPE=Release -DZONOS2_APP=ON -DGGML_VULKAN=ON -DBUILD_SHARED_LIBS=OFF + -DCMAKE_C_COMPILER=gcc-14 -DCMAKE_CXX_COMPILER=g++-14 + -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache + - name: Build + run: cmake --build build -j"$(nproc)" + - name: Smoke test + shell: bash + run: | + bin=$(find build -name zonos2-app -type f | head -1) + out=$("$bin" --help 2>&1 || true) + echo "$out" | grep -q "usage:" || { echo "app smoke failed:"; echo "$out"; exit 1; } + - name: Package + if: startsWith(github.ref, 'refs/tags/v') + shell: bash + run: | + mkdir -p dist + find build -name 'zonos2-app' -type f -exec cp {} dist/ \; + cp -r web dist/web + cp assets/app/zonos2.desktop assets/app/zonos2.png dist/ + tar -C dist -czf zonos2-linux-x64-app.tar.gz . + - uses: actions/upload-artifact@v4 + if: startsWith(github.ref, 'refs/tags/v') + with: + name: zonos2-linux-x64-app + path: zonos2-linux-x64-app.tar.gz + # ------------------------------------------------------------- macOS Metal - # macos-14 is Apple Silicon (arm64); Metal is enabled by default in ggml. + # macos-15 is Apple Silicon (arm64); Metal is enabled by default in ggml. + # (15 rather than 14 because zonos2-app's saucer dependency needs AppleClang 17 + # / Xcode >= 16.3.) macos-metal: - runs-on: macos-14 + runs-on: macos-15 steps: - uses: actions/checkout@v4 with: @@ -156,12 +228,22 @@ jobs: - uses: hendrikmuhs/ccache-action@v1.2 with: key: macos-metal + - name: Select Xcode >= 16.3 (saucer needs AppleClang 17) + run: | + sudo xcode-select -s "$(ls -d /Applications/Xcode_16.*.app /Applications/Xcode_2*.app 2>/dev/null | sort -V | tail -1)/Contents/Developer" + clang++ --version - name: Configure run: > - cmake -B build -DCMAKE_BUILD_TYPE=Release -DBUILD_SHARED_LIBS=OFF + cmake -B build -DCMAKE_BUILD_TYPE=Release -DZONOS2_APP=ON -DBUILD_SHARED_LIBS=OFF -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache - name: Build run: cmake --build build -j"$(sysctl -n hw.ncpu)" + - name: App smoke test + shell: bash + run: | + bin=$(find build -path '*Zonos2.app/Contents/MacOS/*' -type f -perm +111 | head -1) + out=$("$bin" --help 2>&1 || true) + echo "$out" | grep -q "usage:" || { echo "app smoke failed:"; echo "$out"; exit 1; } - name: Smoke test shell: bash run: | @@ -186,6 +268,11 @@ jobs: - name: README command tests shell: bash run: bash tests/readme-commands.sh + # Exercise the end-user launcher (download-if-missing + serve + teardown) + # against the dummy fixtures and a local http.server standing in for HF. + - name: Launcher smoke test + shell: bash + run: bash tests/launcher-smoke.sh - name: Package if: startsWith(github.ref, 'refs/tags/v') shell: bash @@ -196,12 +283,27 @@ jobs: cp -r default_voices emotion_directions dist/ mkdir -p dist/scripts cp scripts/normalize-text.py dist/scripts/ + # stamp the launcher's backend default to match this archive's build; + # ship it twice: .command for Finder double-click, .sh for the terminal + sed 's/^GPU_DEFAULT=cpu/GPU_DEFAULT=gpu/' scripts/start-zonos2.sh > dist/start-zonos2.sh + grep -q '^GPU_DEFAULT=gpu' dist/start-zonos2.sh || { echo "GPU stamp failed"; exit 1; } + cp dist/start-zonos2.sh dist/start-zonos2.command + chmod +x dist/start-zonos2.sh dist/start-zonos2.command tar -C dist -czf zonos2-macos-arm64-metal.tar.gz . + # Desktop app bundle, ad-hoc signed (users right-click > Open past Gatekeeper). + app=$(find build -name 'Zonos2.app' -type d | head -1) + codesign --force --deep -s - "$app" + ditto -c -k --keepParent "$app" zonos2-macos-arm64-app.zip - uses: actions/upload-artifact@v4 if: startsWith(github.ref, 'refs/tags/v') with: name: zonos2-macos-arm64-metal path: zonos2-macos-arm64-metal.tar.gz + - uses: actions/upload-artifact@v4 + if: startsWith(github.ref, 'refs/tags/v') + with: + name: zonos2-macos-arm64-app + path: zonos2-macos-arm64-app.zip # ----------------------------------------------------------- Windows Vulkan # MSVC build; relies on the _WIN32 file-IO shim in src/compat.h. @@ -260,10 +362,16 @@ jobs: win-vulkan-build-${{ steps.ggml.outputs.sha }}- - name: Configure run: > - cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DGGML_VULKAN=ON -DBUILD_SHARED_LIBS=OFF + cmake -B build -G Ninja -DCMAKE_BUILD_TYPE=Release -DZONOS2_APP=ON -DGGML_VULKAN=ON -DBUILD_SHARED_LIBS=OFF -DCMAKE_C_COMPILER_LAUNCHER=ccache -DCMAKE_CXX_COMPILER_LAUNCHER=ccache - name: Build run: cmake --build build -j + - name: App smoke test + shell: bash + run: | + bin=$(find build -name zonos2-app.exe -type f | head -1) + out=$("$bin" --help 2>&1 || true) + echo "$out" | grep -q "usage:" || { echo "app smoke failed:"; echo "$out"; exit 1; } - name: Smoke test shell: bash run: | @@ -288,26 +396,41 @@ jobs: - name: README command tests shell: bash run: bash tests/readme-commands.sh + # The launcher can't run end-to-end here (no bash trap semantics; see + # tests/launcher-smoke.sh for the Linux/macOS coverage), but a parse check + # catches ps1 syntax errors before they ship in a release archive. + - name: Launcher parse check + shell: pwsh + run: | + $errs = $null + [System.Management.Automation.Language.Parser]::ParseFile("$PWD\scripts\start-zonos2.ps1", [ref]$null, [ref]$errs) | Out-Null + if ($errs) { $errs; throw "start-zonos2.ps1 has parse errors" } - name: Package if: startsWith(github.ref, 'refs/tags/v') shell: bash run: | mkdir -p dist - find build \( -name 'zonos2-cli.exe' -o -name 'spk-encoder-cli.exe' -o -name 'dac-cli.exe' -o -name 'quantize-cli.exe' -o -name 'zonos2-server.exe' \) -type f -exec cp {} dist/ \; + find build \( -name 'zonos2-cli.exe' -o -name 'spk-encoder-cli.exe' -o -name 'dac-cli.exe' -o -name 'quantize-cli.exe' -o -name 'zonos2-server.exe' -o -name 'zonos2-app.exe' \) -type f -exec cp {} dist/ \; cp -r web dist/web cp -r default_voices emotion_directions dist/ mkdir -p dist/scripts cp scripts/normalize-text.py dist/scripts/ - tar -C dist -czf zonos2-windows-x64-vulkan.tar.gz . + # stamp the launcher's backend default to match this archive's build + sed "s/^\\\$GPU_DEFAULT = 'cpu'/\$GPU_DEFAULT = 'gpu'/" scripts/start-zonos2.ps1 > dist/start-zonos2.ps1 + grep -q "^\\\$GPU_DEFAULT = 'gpu'" dist/start-zonos2.ps1 || { echo "GPU stamp failed"; exit 1; } + cp scripts/start-zonos2.bat dist/start-zonos2.bat + # zip, not tar.gz: Explorer extracts it natively and (unlike tar) keeps + # the Mark-of-the-Web flow the README's SmartScreen guidance describes + (cd dist && 7z a -tzip ../zonos2-windows-x64-vulkan.zip . > /dev/null) - uses: actions/upload-artifact@v4 if: startsWith(github.ref, 'refs/tags/v') with: name: zonos2-windows-x64-vulkan - path: zonos2-windows-x64-vulkan.tar.gz + path: zonos2-windows-x64-vulkan.zip # ------------------------------------------------- Release (version tags only) release: - needs: [linux-cpu, linux-vulkan, macos-metal, windows-vulkan] + needs: [linux-cpu, linux-vulkan, linux-app, macos-metal, windows-vulkan] if: startsWith(github.ref, 'refs/tags/v') runs-on: ubuntu-latest steps: diff --git a/.gitignore b/.gitignore index c94380f..9e5cb15 100644 --- a/.gitignore +++ b/.gitignore @@ -16,5 +16,8 @@ __pycache__/ .venv-tts-norm/ _pydeps/ +# zonos2-app (saucer) runtime state — cookies/localStorage SQLite db, not source +.saucer + # Personal Claude Code instructions (private) CLAUDE.local.md diff --git a/CMakeLists.txt b/CMakeLists.txt index e1e7a6c..3fb7005 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,6 +10,22 @@ if (NOT CMAKE_BUILD_TYPE) set(CMAKE_BUILD_TYPE Release CACHE STRING "" FORCE) endif() +# --- zonos2-app (desktop shell) opt-in. Declared here, before add_subdirectory(ggml), +# specifically so the Apple Objective-C / -C++ languages it needs are enabled in +# THIS root scope first. saucer's WKWebView backend + saucer-embed inject .m/.mm +# sources into the top-level zonos2-app target; ggml's Metal backend also enables +# OBJC — but inside ggml's own subdirectory scope. Whichever scope enables a +# language *first* is the only one that gets CMAKE__COMPILE_OBJECT, so if +# ggml (a subdir) wins the race the top-level target is left without the rule and +# CMake dies at generate with "CMAKE_OBJC_COMPILE_OBJECT not set". Enabling both +# here, pre-ggml, makes the root scope the first enabler and the rules propagate +# down to ggml and up to zonos2-app alike. C++23 toolchain still required (README). --- +option(ZONOS2_APP "Build the zonos2-app desktop shell (requires a C++23 toolchain, see README)" OFF) +if (ZONOS2_APP AND APPLE) + enable_language(OBJC) + enable_language(OBJCXX) +endif() + # --- CUDA target (H100 = sm_90). Enable with -DGGML_CUDA=ON --- if (GGML_CUDA) if (NOT DEFINED CMAKE_CUDA_ARCHITECTURES) @@ -81,6 +97,11 @@ add_executable(quantize-cli src/quantize.cpp) target_include_directories(quantize-cli PRIVATE src ggml/include) target_link_libraries(quantize-cli PRIVATE ggml) +# --- gguf -> gguf MoE expert pruner (drops low-MSAN experts per layer) --- +add_executable(prune-cli src/prune.cpp) +target_include_directories(prune-cli PRIVATE src ggml/include) +target_link_libraries(prune-cli PRIVATE ggml) + # --- perplexity / KL-divergence eval for quantized backbones (scoring only, no DAC) --- add_executable(zonos2-perplexity src/perplexity.cpp) target_include_directories(zonos2-perplexity PRIVATE src ggml/include) @@ -95,3 +116,58 @@ target_link_libraries(zonos2-server PRIVATE zonos2 ggml Threads::Threads) if (WIN32) target_link_libraries(zonos2-server PRIVATE ws2_32) endif() + +# --- Desktop app: the server embedded in a native webview window (saucer). Opt-in +# because saucer needs a much newer toolchain than the rest of the project: +# CMake >= 3.25, C++23 (GCC >= 14 / Clang >= 20 / AppleClang >= 17 / MSVC >= 19.44), +# and on Linux GTK4 >= 4.12 + WebKitGTK-6.0 + libadwaita + json-glib dev packages. +# Everything else in this file keeps building with the C++17 / CMake 3.18 floor. --- +# ZONOS2_APP is declared (and its Apple languages enabled) near the top of this file, +# before add_subdirectory(ggml); see the comment there for why the ordering matters. +if (ZONOS2_APP) + if (CMAKE_VERSION VERSION_LESS 3.31) + # saucer core says 3.25, but its saucer-embed dependency uses policy CMP0174 (3.31). + message(FATAL_ERROR "ZONOS2_APP requires CMake >= 3.31 (saucer); this is CMake ${CMAKE_VERSION}") + endif() + + include(FetchContent) + FetchContent_Declare(saucer + GIT_REPOSITORY https://github.com/saucer/saucer + GIT_TAG v8.0.5) + FetchContent_Declare(saucer-desktop # native file pickers (saucer module) + GIT_REPOSITORY https://github.com/saucer/desktop + GIT_TAG v4.1.0) + FetchContent_MakeAvailable(saucer saucer-desktop) + + add_executable(zonos2-app src/app.cpp src/server.cpp src/dac.cpp src/spk-encoder.cpp) + target_compile_definitions(zonos2-app PRIVATE ZONOS2_APP_EMBED) # server.cpp: no main() + target_compile_features(zonos2-app PRIVATE cxx_std_23) + target_include_directories(zonos2-app PRIVATE src vendor ggml/include) + target_link_libraries(zonos2-app PRIVATE zonos2 ggml saucer::saucer saucer::desktop Threads::Threads) + + if (WIN32) + # GUI subsystem (no console window) with the icon resource; saucer has no + # WinMain, so keep the standard main() as the entry point. + target_sources(zonos2-app PRIVATE assets/app/zonos2.rc) + set_target_properties(zonos2-app PROPERTIES WIN32_EXECUTABLE TRUE) + target_link_libraries(zonos2-app PRIVATE ws2_32) + if (MSVC) + target_link_options(zonos2-app PRIVATE "/ENTRY:mainCRTStartup") + endif() + elseif (APPLE) + # Zonos2.app bundle; the web UI and icon ship inside Contents/Resources + # (app.cpp resolves ../Resources/tts_ui.html relative to the executable). + set(ZONOS2_APP_RESOURCES web/tts_ui.html assets/app/zonos2.icns assets/app/zonos2.png) + target_sources(zonos2-app PRIVATE ${ZONOS2_APP_RESOURCES}) + set_source_files_properties(${ZONOS2_APP_RESOURCES} PROPERTIES MACOSX_PACKAGE_LOCATION Resources) + set_target_properties(zonos2-app PROPERTIES + OUTPUT_NAME Zonos2 + MACOSX_BUNDLE TRUE + MACOSX_BUNDLE_INFO_PLIST ${CMAKE_CURRENT_SOURCE_DIR}/assets/app/Info.plist.in + MACOSX_BUNDLE_BUNDLE_NAME Zonos2 + MACOSX_BUNDLE_GUI_IDENTIFIER cpp.zonos2.app + MACOSX_BUNDLE_BUNDLE_VERSION 1.0 + MACOSX_BUNDLE_SHORT_VERSION_STRING 1.0 + MACOSX_BUNDLE_ICON_FILE zonos2.icns) + endif() +endif() diff --git a/README.md b/README.md index e6c2697..adf373d 100644 --- a/README.md +++ b/README.md @@ -41,23 +41,14 @@ decoder bit-exact, the backbone to cosine ≥ 0.9999. ## Quick Start > **Platform Support**: Linux x64 (CPU / Vulkan), macOS arm64 (Metal), and Windows x64 (Vulkan) -> ship prebuilt. For CUDA (sm_90 / H100 by default), build from source. +> ship prebuilt. For CUDA (sm_90 / H100 by default), [build from source](#manual--advanced-setup). -### 1. Installation +### 1. Download & extract -Grab a self-contained build from the [Releases](../../releases) page (each archive holds -`zonos2-server` + the `web/` UI, `zonos2-cli`, `spk-encoder-cli`, `dac-cli`, and `quantize-cli` — -no shared-library install), or build from source: - -```bash -git clone --recurse-submodules https://github.com/Zyphra/zonos2.cpp.git && cd zonos2.cpp - -# CPU -cmake -B build -DCMAKE_BUILD_TYPE=Release && cmake --build build -j - -# CUDA (H100 = sm_90; pass -DCMAKE_CUDA_ARCHITECTURES= for other GPUs) -cmake -B build-cuda -DGGML_CUDA=ON -DCMAKE_BUILD_TYPE=Release && cmake --build build-cuda -j -``` +Grab the archive for your OS from the [Releases](../../releases) page (a `.tar.gz`, or a `.zip` +on Windows — extract it anywhere). Each archive is self-contained: the `start-zonos2` launcher, +`zonos2-server` + the `web/` UI, and the `zonos2-cli` / `spk-encoder-cli` / `dac-cli` / +`quantize-cli` tools — no installer, no shared libraries. > **First run on macOS / Windows:** the release binaries are unsigned (built by CI, with no > code-signing certificate), so the OS may block them the *first* time you run one. This is @@ -69,88 +60,71 @@ cmake -B build-cuda -DGGML_CUDA=ON -DCMAKE_BUILD_TYPE=Release && cmake --build b - **macOS (Gatekeeper)** — you'll see *"…cannot be opened because the developer cannot be verified"* (or *"Apple could not verify … is free of malware"* on Sequoia). macOS flags every - file in a downloaded archive with a quarantine attribute; strip it from the folder you extracted - into, then run the tools normally: + file in a downloaded archive with a quarantine attribute — including `start-zonos2.command`; + strip it from the folder you extracted into, then run the tools normally: ```bash xattr -dr com.apple.quarantine /path/to/extracted-folder ``` Alternatively, after the first blocked launch, allow it via **System Settings → Privacy & - Security → Open Anyway**. + Security → Open Anyway** (or right-click the `.command` → **Open**). If the double-click stays + blocked, run it from a terminal instead: `bash start-zonos2.command`. - **Windows (SmartScreen / Defender)** — *"Windows protected your PC"* appears the first time you - launch `zonos2-server.exe` / `zonos2-cli.exe`. Click **More info → Run anyway**. To clear the + launch `start-zonos2.bat` or one of the `.exe`s. Click **More info → Run anyway**. To clear the block before extracting, unblock the downloaded archive (right-click → **Properties → Unblock → OK**, or in PowerShell): ```powershell - Unblock-File .\zonos2-windows-x64-vulkan.tar.gz + Unblock-File .\zonos2-windows-x64-vulkan.zip ``` If Microsoft Defender quarantines the unrecognized `.exe`, restore it from Protection History - or add a Defender exclusion for the folder. + or add a Defender exclusion for the folder. On enterprise machines where PowerShell scripts are + locked down entirely, launch the server by hand instead — see + [Manual & advanced setup](#manual--advanced-setup). - **Linux** — no signing prompt. If a binary won't start, it usually just needs the execute bit: ```bash - chmod +x zonos2-cli zonos2-server + chmod +x start-zonos2.sh zonos2-cli zonos2-server ``` -### 2. Install ffmpeg (required for voice cloning) +### 2. Run the start script -Voice cloning decodes reference audio by shelling out to `ffmpeg` — the server's `--spk` -upload route, `zonos2-cli --clone`, and `spk-encoder-cli --clone` all need it on your `PATH`. -Plain TTS works without it. +| OS | Do this | +|---|---| +| Linux | `./start-zonos2.sh` | +| macOS | double-click `start-zonos2.command` (or `./start-zonos2.sh`) | +| Windows | double-click `start-zonos2.bat` | -```bash -# macOS -brew install ffmpeg -# Debian / Ubuntu -sudo apt install ffmpeg -# Windows -winget install ffmpeg # or: choco install ffmpeg / scoop install ffmpeg -``` +The first run downloads the models (~7.1 GB — the Q6_K backbone plus the DAC vocoder and speaker +encoder) into `models/` next to the script; downloads resume if interrupted. It then launches +`zonos2-server` and opens the web UI at `http://127.0.0.1:1919/`. Later runs skip straight to +launch. The Vulkan/Metal archives default to GPU, the CPU archive to CPU. Close the window (or +Ctrl-C) to stop the server. -### 3. Get the Models +For **voice cloning**, a static `ffmpeg` (used only to decode your reference audio — plain TTS +never needs it) is fetched into `models/bin/` on first run unless one is already on your `PATH`. +Linux/Windows use the [BtbN](https://github.com/BtbN/FFmpeg-Builds) LGPL build; macOS uses a +pinned static [arm64 build](https://github.com/eugeneware/ffmpeg-static) (GPL — nothing is +redistributed here, your machine downloads it from the provider, and it runs as a separate +process). The desktop app fetches its own copy the same way. Set `ZONOS2_FFMPEG=/path/to/ffmpeg` +to point at your own instead. -Pull the ready-made GGUFs from [`Zyphra/ZONOS2-GGUF`](https://huggingface.co/Zyphra/ZONOS2-GGUF): +Common knobs (`./start-zonos2.sh --help` / `start-zonos2.bat -Help` lists everything): -```bash -hf download Zyphra/ZONOS2-GGUF dac.gguf spk-encoder.gguf --local-dir out -# pick a backbone: Q8_0 is effectively lossless, Q4_K is the smallest that holds quality -hf download Zyphra/ZONOS2-GGUF zonos2-q8_0.gguf --local-dir out # 8.5 GB -hf download Zyphra/ZONOS2-GGUF zonos2-q4_k.gguf --local-dir out # 4.9 GB -``` - -(You can also convert the original checkpoints and make any quant yourself — see -[docs/INTERNALS.md](docs/INTERNALS.md).) +| | Linux/macOS | Windows | +|---|---|---| +| Backbone quant (q4_k 4.9 GB · q5_k 5.8 GB · q6_k 6.8 GB · q8_0 8.5 GB · f16 15.3 GB) | `--quant q4_k` | `-Quant q4_k` | +| Force backend | `--cpu` / `--gpu` | `-Cpu` / `-Gpu` | +| Port | `--port 8080` | `-Port 8080` | +| Skip download prompt / browser | `-y`, `--no-browser` | `-Yes`, `-NoBrowser` | +| Extra `zonos2-server` flags | `-- --batch 16` | appended verbatim | -### 4. Launch the TTS Server +Model dir, download URL, and more can be overridden via `ZONOS2_*` environment variables (see +`--help`). Prefer to launch the server by hand, or want CUDA? See +[Manual & advanced setup](#manual--advanced-setup). -```bash -zonos2-server out/zonos2-q8_0.gguf --dac out/dac.gguf --spk out/spk-encoder.gguf \ - --host 0.0.0.0 --port 1919 --gpu --tts-default-voices-dir default_voices -``` - -The server starts on `http://localhost:1919` by default. It loads the backbone + DAC (+ speaker -encoder) once and serves the reference TTS API: streaming float32 PCM, the OpenAI-compatible -`/v1/audio/speech` route, audio-upload voice cloning, cached/default speakers, and SLERP speaker -blending. `--spk` enables cloning from uploaded reference audio and default voice audio files; -drop `--gpu` for CPU. Emotion direction files in `./emotion_directions/` are autoloaded when -present; use `--tts-emotion-directions-dir ` to point elsewhere or pass an empty directory -string to disable emotion controls. Default voices are scanned from `default_voices/` by default; -use `--tts-default-voices-dir ` to point elsewhere or pass an empty string to disable them. - -Text normalization is available through the reference NeMo/Pynini helper when launched with a -Python environment that has `pynini` installed: - -```bash -zonos2-server out/zonos2-q8_0.gguf --dac out/dac.gguf --spk out/spk-encoder.gguf \ - --gpu --text-normalizer-python .venv-tts-norm/bin/python -``` - -Without `--text-normalizer-python`, the server keeps the pure byte-level tokenizer path and reports -`text_normalization_enabled:false`. - -### 5. Generate Speech +### 3. Generate Speech **curl:** @@ -171,6 +145,31 @@ curl -s http://localhost:1919/tts/generate \ **Web UI:** Open `http://localhost:1919/` in your browser. +## Desktop App (zonos2-app) + +The same server and web UI, packaged as a native window (a [saucer](https://github.com/saucer/saucer) +webview: WebView2 / WKWebView / WebKitGTK). First launch opens a setup page to pick your `.gguf` +files with native file dialogs; the choice is saved (`~/.config/zonos2/app.json`, or the platform +equivalent) and later launches boot straight into the UI. The embedded server binds an ephemeral +`127.0.0.1` port and its whole HTTP API stays reachable while the app runs; closing the window +shuts it down. Release tags ship prebuilt bundles: `zonos2-linux-x64-app.tar.gz`, +`zonos2-macos-arm64-app.zip` (a `Zonos2.app`), and `zonos2-app.exe` inside the Windows +`zonos2-windows-x64-vulkan.zip`. + +Building it is opt-in — saucer needs a much newer toolchain than the rest of the project +(CMake ≥ 3.31 and a C++23 compiler: GCC ≥ 14 / Clang ≥ 20 / Xcode ≥ 16.3 / MSVC ≥ 19.44): + +```bash +# Linux additionally needs: libgtk-4-dev (>= 4.12, i.e. Ubuntu 24.04+), libwebkitgtk-6.0-dev, +# libadwaita-1-dev, libjson-glib-dev +cmake -B build -DZONOS2_APP=ON +cmake --build build --target zonos2-app + +./build/zonos2-app # setup page on first run, saved config afterwards +./build/zonos2-app --setup # reopen the setup page +./build/zonos2-app out/zonos2-q8_0.gguf --dac out/dac.gguf --gpu # or pass zonos2-server args through +``` + ## Emotion Control Emotion control nudges a cloned speaker voice with shipped direction vectors: named sliders @@ -206,7 +205,9 @@ zonos2-cli out/zonos2-q8_0.gguf --tts "Hello, world." out.wav \ --dac out/dac.gguf --gpu --seed 1 ``` -Clone a voice in-process by pointing `--clone` at any reference audio: +Clone a voice in-process by pointing `--clone` at any reference audio. The standalone `zonos2-cli` +resolves `ffmpeg` from `$ZONOS2_FFMPEG`, then next to the binary, then `PATH` (unlike the start +script / desktop app, it does not auto-download one — install ffmpeg or point `ZONOS2_FFMPEG` at one): ```bash zonos2-cli out/zonos2-q8_0.gguf --tts "Cloned voice demo." out.wav \ @@ -221,6 +222,74 @@ zonos2-cli out/zonos2-q8_0.gguf --tts "That was incredible." out.wav \ --emotion-strength 1 --emotion-cfg-scale 1.5 --gpu --seed 1 ``` +When `dac.gguf` / `spk-encoder.gguf` sit next to the backbone GGUF (as the start script arranges +in `models/`), `--dac` and `--spk-encoder` are found automatically and can be dropped: + +```bash +zonos2-cli models/zonos2-q6_k.gguf --tts "Hello, world." out.wav --gpu --seed 1 +``` + +## Manual & advanced setup + +### Download models by hand + +The launcher just fetches from [`Zyphra/ZONOS2-GGUF`](https://huggingface.co/Zyphra/ZONOS2-GGUF); +you can do the same with the HF CLI (or plain `curl`): + +```bash +hf download Zyphra/ZONOS2-GGUF dac.gguf spk-encoder.gguf --local-dir out +# pick a backbone: Q8_0 is effectively lossless, Q4_K is the smallest that holds quality +hf download Zyphra/ZONOS2-GGUF zonos2-q8_0.gguf --local-dir out # 8.5 GB +hf download Zyphra/ZONOS2-GGUF zonos2-q4_k.gguf --local-dir out # 4.9 GB +``` + +(You can also convert the original checkpoints and make any quant yourself — see +[docs/INTERNALS.md](docs/INTERNALS.md).) + +### Launch the server by hand + +```bash +zonos2-server out/zonos2-q8_0.gguf --dac out/dac.gguf --spk out/spk-encoder.gguf \ + --host 0.0.0.0 --port 1919 --gpu --tts-default-voices-dir default_voices +``` + +The server starts on `http://localhost:1919` by default. It loads the backbone + DAC (+ speaker +encoder) once and serves the reference TTS API: streaming float32 PCM, the OpenAI-compatible +`/v1/audio/speech` route, audio-upload voice cloning, cached/default speakers, and SLERP speaker +blending. `--spk` (alias `--spk-encoder`) enables cloning from uploaded reference audio and +default voice audio files; drop `--gpu` for CPU. As with the CLI, `--dac`/`--spk` are +auto-detected when the GGUFs sit next to the backbone. Emotion direction files in +`./emotion_directions/` are autoloaded when present; use `--tts-emotion-directions-dir ` to +point elsewhere or pass an empty directory string to disable emotion controls. Default voices are +scanned from `default_voices/` by default; use `--tts-default-voices-dir ` to point +elsewhere or pass an empty string to disable them. `zonos2-server` with no arguments prints all +options (`--host`, `--batch`, streaming knobs, …). + +Text normalization is available through the reference NeMo/Pynini helper when launched with a +Python environment that has `pynini` installed: + +```bash +zonos2-server out/zonos2-q8_0.gguf --dac out/dac.gguf --spk out/spk-encoder.gguf \ + --gpu --text-normalizer-python .venv-tts-norm/bin/python +``` + +Without `--text-normalizer-python`, the server keeps the pure byte-level tokenizer path and reports +`text_normalization_enabled:false`. + +### Build from source + +```bash +git clone --recurse-submodules https://github.com/Zyphra/zonos2.cpp.git && cd zonos2.cpp + +# CPU +cmake -B build -DCMAKE_BUILD_TYPE=Release && cmake --build build -j + +# CUDA (H100 = sm_90; pass -DCMAKE_CUDA_ARCHITECTURES= for other GPUs) +cmake -B build-cuda -DGGML_CUDA=ON -DCMAKE_BUILD_TYPE=Release && cmake --build build-cuda -j +``` + +Vulkan and Metal build the same way (`-DGGML_VULKAN=ON`; Metal is on by default on macOS). + ## Under the Hood zonos2.cpp is a faithful, numerically-validated port. For the full conversion flow, the diff --git a/assets/app/Info.plist.in b/assets/app/Info.plist.in new file mode 100644 index 0000000..6d55764 --- /dev/null +++ b/assets/app/Info.plist.in @@ -0,0 +1,30 @@ + + + + + CFBundleDevelopmentRegion + English + CFBundleExecutable + ${MACOSX_BUNDLE_EXECUTABLE_NAME} + CFBundleGetInfoString + ${MACOSX_BUNDLE_INFO_STRING} + CFBundleIconFile + ${MACOSX_BUNDLE_ICON_FILE} + CFBundleIdentifier + ${MACOSX_BUNDLE_GUI_IDENTIFIER} + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + ${MACOSX_BUNDLE_BUNDLE_NAME} + CFBundlePackageType + APPL + CFBundleShortVersionString + ${MACOSX_BUNDLE_SHORT_VERSION_STRING} + CFBundleVersion + ${MACOSX_BUNDLE_BUNDLE_VERSION} + NSHighResolutionCapable + + NSHumanReadableCopyright + ${MACOSX_BUNDLE_COPYRIGHT} + + diff --git a/assets/app/zonos2.desktop b/assets/app/zonos2.desktop new file mode 100644 index 0000000..267cc0e --- /dev/null +++ b/assets/app/zonos2.desktop @@ -0,0 +1,9 @@ +[Desktop Entry] +Type=Application +Name=Zonos2 +Comment=Zonos2 text-to-speech +Exec=zonos2-app +Icon=zonos2 +Terminal=false +Categories=AudioVideo;Audio;Utility; +StartupWMClass=zonos2 diff --git a/assets/app/zonos2.icns b/assets/app/zonos2.icns new file mode 100644 index 0000000..7b497f8 Binary files /dev/null and b/assets/app/zonos2.icns differ diff --git a/assets/app/zonos2.ico b/assets/app/zonos2.ico new file mode 100644 index 0000000..db05daf Binary files /dev/null and b/assets/app/zonos2.ico differ diff --git a/assets/app/zonos2.png b/assets/app/zonos2.png new file mode 100644 index 0000000..cdf820c Binary files /dev/null and b/assets/app/zonos2.png differ diff --git a/assets/app/zonos2.rc b/assets/app/zonos2.rc new file mode 100644 index 0000000..46dfe25 --- /dev/null +++ b/assets/app/zonos2.rc @@ -0,0 +1,2 @@ +// Windows resources for zonos2-app: the exe/taskbar icon. +IDI_ICON1 ICON "zonos2.ico" diff --git a/scripts/make-app-icon.py b/scripts/make-app-icon.py new file mode 100644 index 0000000..dd3d252 --- /dev/null +++ b/scripts/make-app-icon.py @@ -0,0 +1,90 @@ +#!/usr/bin/env python3 +"""Generate the zonos2-app icon set (assets/app/zonos2.{png,ico,icns}). + +Draws a 1024x1024 mark in the style of assets/ZONOS2BlogThumbnail.png (near-black +field, orange smoke wave, thin silver "Z2") — the thumbnail itself is 1200x628 +wordmark art and doesn't crop to a square. Pillow renders all three outputs, so +this runs on any platform (no iconutil/ImageMagick needed). + + python3 scripts/make-app-icon.py [outdir=assets/app] +""" +import math +import sys +from pathlib import Path + +from PIL import Image, ImageDraw, ImageFilter, ImageFont + +SIZE = 1024 +FONT_CANDIDATES = [ + "/usr/share/fonts/truetype/lato/Lato-Light.ttf", + "/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", + "/System/Library/Fonts/HelveticaNeue.ttc", + "C:/Windows/Fonts/segoeuil.ttf", +] + + +def load_font(px: int) -> ImageFont.FreeTypeFont: + for cand in FONT_CANDIDATES: + if Path(cand).exists(): + return ImageFont.truetype(cand, px) + return ImageFont.load_default(px) + + +def wave_layer(color, amp, phase, thickness, blur, alpha): + """One blurred sine ribbon across the lower half, like the thumbnail's smoke.""" + layer = Image.new("RGBA", (SIZE, SIZE), (0, 0, 0, 0)) + d = ImageDraw.Draw(layer) + pts = [] + for x in range(-64, SIZE + 65, 16): + y = SIZE * 0.68 + amp * math.sin(x / SIZE * 2.2 * math.pi + phase) + pts.append((x, y)) + for off in range(-thickness // 2, thickness // 2 + 1, 6): + d.line([(x, y + off) for x, y in pts], fill=color + (alpha,), width=8) + return layer.filter(ImageFilter.GaussianBlur(blur)) + + +def make_master() -> Image.Image: + img = Image.new("RGBA", (SIZE, SIZE), (10, 11, 14, 255)) + + # subtle radial glow behind the mark + glow = Image.new("L", (SIZE, SIZE), 0) + ImageDraw.Draw(glow).ellipse((112, 112, SIZE - 112, SIZE - 112), fill=46) + img.paste(Image.new("RGBA", (SIZE, SIZE), (46, 42, 36, 255)), (0, 0), + glow.filter(ImageFilter.GaussianBlur(180))) + + # orange smoke waves (front) + a faint silver one (back) + img.alpha_composite(wave_layer((190, 190, 200), 120, 2.4, 70, 60, 60)) + img.alpha_composite(wave_layer((222, 130, 28), 96, 0.0, 90, 40, 130)) + img.alpha_composite(wave_layer((255, 170, 60), 110, 0.9, 40, 18, 160)) + + # thin silver "Z2", slightly letterspaced, optically centered in the upper field + font = load_font(430) + text = "Z2" + tracking = 40 + widths = [ImageDraw.Draw(img).textlength(c, font=font) for c in text] + total = sum(widths) + tracking * (len(text) - 1) + x = (SIZE - total) / 2 + top = SIZE * 0.42 + grad = Image.new("RGBA", (SIZE, SIZE), (0, 0, 0, 0)) + gd = ImageDraw.Draw(grad) + for i, c in enumerate(text): + gd.text((x, top), c, font=font, fill=(233, 235, 240, 255), anchor="lm") + x += widths[i] + tracking + img.alpha_composite(grad) + return img + + +def main() -> None: + outdir = Path(sys.argv[1]) if len(sys.argv) > 1 else Path("assets/app") + outdir.mkdir(parents=True, exist_ok=True) + master = make_master() + + master.save(outdir / "zonos2.png") + master.save(outdir / "zonos2.ico", sizes=[(s, s) for s in (16, 24, 32, 48, 64, 128, 256)]) + # Pillow's ICNS writer derives all required sizes from the 1024px master. + master.save(outdir / "zonos2.icns") + print(f"wrote {outdir}/zonos2.png .ico .icns") + + +if __name__ == "__main__": + main() diff --git a/scripts/start-zonos2.bat b/scripts/start-zonos2.bat new file mode 100644 index 0000000..eb773e2 --- /dev/null +++ b/scripts/start-zonos2.bat @@ -0,0 +1,4 @@ +@echo off +rem start-zonos2 — double-click to download the models (if missing) and launch the server. +powershell -NoProfile -ExecutionPolicy Bypass -File "%~dp0start-zonos2.ps1" %* +if errorlevel 1 pause diff --git a/scripts/start-zonos2.ps1 b/scripts/start-zonos2.ps1 new file mode 100644 index 0000000..6e99d44 --- /dev/null +++ b/scripts/start-zonos2.ps1 @@ -0,0 +1,200 @@ +# start-zonos2.ps1 — download the models if missing, launch zonos2-server, open the browser. +# Launched by start-zonos2.bat (double-click) with -ExecutionPolicy Bypass; mirrors +# scripts/start-zonos2.sh — keep the two in sync. +param( + [string]$Quant, + [switch]$Cpu, + [switch]$Gpu, + [string]$BindHost, + [int]$Port, + [switch]$Yes, + [switch]$NoBrowser, + [switch]$Help, + [Parameter(ValueFromRemainingArguments = $true)][string[]]$ExtraArgs +) +$ErrorActionPreference = 'Stop' + +$GPU_DEFAULT = 'cpu' # CI stamps this to 'gpu' in the vulkan release archive +$ScriptDir = $PSScriptRoot + +if (-not $Quant) { $Quant = if ($env:ZONOS2_QUANT) { $env:ZONOS2_QUANT } else { 'q6_k' } } +$ModelDir = if ($env:ZONOS2_MODEL_DIR) { $env:ZONOS2_MODEL_DIR } else { Join-Path $ScriptDir 'models' } +$BaseUrl = if ($env:ZONOS2_BASE_URL) { $env:ZONOS2_BASE_URL } else { 'https://huggingface.co/Zyphra/ZONOS2-GGUF/resolve/main' } +if (-not $BindHost) { $BindHost = if ($env:ZONOS2_HOST) { $env:ZONOS2_HOST } else { '127.0.0.1' } } +if (-not $Port) { $Port = if ($env:ZONOS2_PORT) { [int]$env:ZONOS2_PORT } else { 1919 } } +$ServerBin = if ($env:ZONOS2_SERVER_BIN) { $env:ZONOS2_SERVER_BIN } else { Join-Path $ScriptDir 'zonos2-server.exe' } +if ($env:ZONOS2_ASSUME_YES) { $Yes = $true } +if ($env:ZONOS2_NO_BROWSER) { $NoBrowser = $true } +$FfmpegUrl = $env:ZONOS2_FFMPEG_URL # override with a single-binary .exe URL (used by tests); default is BtbN LGPL +$FfmpegDir = Join-Path $ModelDir 'bin' +$FfmpegBin = Join-Path $FfmpegDir 'ffmpeg.exe' +$GpuMode = $GPU_DEFAULT +if ($Cpu) { $GpuMode = 'cpu' } +if ($Gpu) { $GpuMode = 'gpu' } + +if ($Help) { + Write-Host @" +usage: start-zonos2.bat [options] [extra zonos2-server args] + -Quant Q model quant to fetch/serve (q4_k 4.9GB, q5_k 5.8GB, q6_k 6.8GB, + q8_0 8.5GB, f16 15.3GB; default q6_k) + -Cpu | -Gpu backend (archive default: $GPU_DEFAULT) + -BindHost H bind address (default 127.0.0.1) + -Port P port (default 1919) + -Yes don't ask before downloading + -NoBrowser don't open the web UI +env overrides: ZONOS2_QUANT ZONOS2_MODEL_DIR ZONOS2_BASE_URL ZONOS2_HOST ZONOS2_PORT + ZONOS2_SERVER_BIN ZONOS2_ASSUME_YES ZONOS2_NO_BROWSER ZONOS2_FFMPEG_URL +ffmpeg (voice cloning only) is fetched into \bin if not already there or on PATH. +"@ + exit 0 +} + +# not Write-Error: under ErrorActionPreference=Stop it throws before reaching exit +function Fail([string]$msg) { Write-Host "start-zonos2: error: $msg" -ForegroundColor Red; exit 1 } + +if (-not (Test-Path $ServerBin)) { + Fail "zonos2-server.exe not found at $ServerBin (run this script from the extracted release archive, or set ZONOS2_SERVER_BIN)" +} + +$Sizes = @{ + 'zonos2-f16.gguf' = '15.3'; 'zonos2-q8_0.gguf' = '8.5'; 'zonos2-q6_k.gguf' = '6.8' + 'zonos2-q5_k.gguf' = '5.8'; 'zonos2-q4_k.gguf' = '4.9'; 'dac.gguf' = '0.25'; 'spk-encoder.gguf' = '0.02' +} +function Get-SizeGB([string]$name) { if ($Sizes.ContainsKey($name)) { $Sizes[$name] } else { '?' } } + +# curl -C - --fail exits 33 when the .part is already complete (HTTP 416): restart clean once. +function Download-One([string]$name) { + $url = "$BaseUrl/$name" + $dst = Join-Path $ModelDir $name + Write-Host "downloading $name ($(Get-SizeGB $name) GB) ..." + & curl.exe -L --fail --retry 3 -C - --progress-bar -o "$dst.part" $url + if ($LASTEXITCODE -eq 33) { + Remove-Item -Force "$dst.part" -ErrorAction SilentlyContinue + & curl.exe -L --fail --retry 3 --progress-bar -o "$dst.part" $url + } + if ($LASTEXITCODE -ne 0) { return $false } + Move-Item -Force "$dst.part" $dst + return $true +} + +# Fetch a static ffmpeg into $FfmpegDir — voice cloning only; basic TTS never uses it. +# Windows uses the BtbN LGPL build (a zip nesting the exe at ffmpeg-*/bin/ffmpeg.exe); +# override with ZONOS2_FFMPEG_URL (single-binary .exe URL). +function Download-Ffmpeg { + New-Item -ItemType Directory -Force -Path $FfmpegDir | Out-Null + if ($FfmpegUrl) { + Write-Host "downloading ffmpeg (for voice cloning) ..." + & curl.exe -L --fail --retry 3 --progress-bar -o "$FfmpegBin.part" $FfmpegUrl + if ($LASTEXITCODE -ne 0) { return $false } + Move-Item -Force "$FfmpegBin.part" $FfmpegBin + return $true + } + $url = 'https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-n7.1-latest-win64-lgpl-7.1.zip' + $zip = Join-Path $FfmpegDir 'ffmpeg-dl.zip' + $ex = Join-Path $FfmpegDir '.extract' + Write-Host "downloading ffmpeg (~90 MB, for voice cloning) ..." + & curl.exe -L --fail --retry 3 --progress-bar -o $zip $url + if ($LASTEXITCODE -ne 0) { return $false } + if (Test-Path $ex) { Remove-Item -Recurse -Force $ex } + Expand-Archive -Path $zip -DestinationPath $ex -Force + $found = Get-ChildItem -Path $ex -Recurse -Filter ffmpeg.exe | + Where-Object { $_.FullName -match '[\\/]bin[\\/]' } | Select-Object -First 1 + if (-not $found) { Remove-Item -Recurse -Force $zip, $ex -ErrorAction SilentlyContinue; return $false } + Move-Item -Force $found.FullName $FfmpegBin + Remove-Item -Recurse -Force $zip, $ex -ErrorAction SilentlyContinue + return (Test-Path $FfmpegBin) +} + +$Backbone = "zonos2-$Quant.gguf" +$Missing = @() +foreach ($f in @($Backbone, 'dac.gguf', 'spk-encoder.gguf')) { + if (-not (Test-Path (Join-Path $ModelDir $f))) { $Missing += $f } +} + +# ffmpeg is only needed for voice cloning: prefer our own copy, then a system ffmpeg +# on PATH, else fetch it alongside the models. +$needFfmpeg = $false +if (Test-Path $FfmpegBin) { + $env:ZONOS2_FFMPEG = $FfmpegBin +} elseif ($env:ZONOS2_SKIP_PATH_FFMPEG -ne '1' -and (Get-Command ffmpeg.exe -ErrorAction SilentlyContinue)) { + # a system ffmpeg is on PATH; the server resolves it there +} else { + $needFfmpeg = $true +} + +if ($Missing.Count -gt 0 -or $needFfmpeg) { + if (-not (Get-Command curl.exe -ErrorAction SilentlyContinue)) { + Fail "curl.exe is required to download models (it ships with Windows 10 1803+) — or place the .gguf files in $ModelDir yourself (see README)" + } + Write-Host "The following will be downloaded into ${ModelDir}:" + foreach ($f in $Missing) { Write-Host " $f ($(Get-SizeGB $f) GB) from $BaseUrl" } + if ($needFfmpeg) { Write-Host " ffmpeg (~90 MB, voice cloning only)" } + if (-not $Yes) { + $ans = Read-Host 'Download now? [Y/n]' + if ($ans -and $ans -notmatch '^[yY]') { Write-Host 'aborted.'; exit 1 } + } + New-Item -ItemType Directory -Force -Path $ModelDir | Out-Null + foreach ($f in $Missing) { + if (-not (Download-One $f)) { + if ($f -eq 'spk-encoder.gguf') { + Write-Warning "failed to download $f — continuing without voice-clone upload support" + } else { + Fail "failed to download $f from $BaseUrl/$f" + } + } + } + if ($needFfmpeg) { + if (Download-Ffmpeg) { $env:ZONOS2_FFMPEG = $FfmpegBin } + else { Write-Warning "ffmpeg download failed — voice cloning disabled (basic TTS still works)" } + } +} + +# The UI, default voices, and emotion directions ship next to this script in release +# archives; in a source checkout the script lives in scripts\ with them one level up. +# Pass them explicitly because the server defaults are cwd-relative and a +# double-clicked .bat may not run from the archive dir. +function Get-Sibling([string]$name) { + $p = Join-Path $ScriptDir $name + if (Test-Path $p) { $p } else { Join-Path $ScriptDir "..\$name" } +} + +$srvArgs = @((Join-Path $ModelDir $Backbone), + '--dac', (Join-Path $ModelDir 'dac.gguf'), + '--host', $BindHost, '--port', $Port, + '--ui', (Get-Sibling 'web\tts_ui.html')) +if (Test-Path (Get-Sibling 'default_voices')) { + $srvArgs += @('--tts-default-voices-dir', (Get-Sibling 'default_voices')) +} +if (Test-Path (Get-Sibling 'emotion_directions')) { + $srvArgs += @('--tts-emotion-directions-dir', (Get-Sibling 'emotion_directions')) +} +if (Test-Path (Join-Path $ModelDir 'spk-encoder.gguf')) { + $srvArgs += @('--spk', (Join-Path $ModelDir 'spk-encoder.gguf')) +} +if ($GpuMode -eq 'gpu') { $srvArgs += '--gpu' } +if ($ExtraArgs) { $srvArgs += $ExtraArgs } + +# PS 5.1 joins -ArgumentList elements with spaces without quoting; quote them ourselves +$quotedArgs = $srvArgs | ForEach-Object { if ("$_" -match '\s') { '"{0}"' -f $_ } else { "$_" } } +$proc = Start-Process -FilePath $ServerBin -ArgumentList $quotedArgs -NoNewWindow -PassThru +try { + $pollHost = if ($BindHost -eq '0.0.0.0' -or $BindHost -eq '::') { '127.0.0.1' } else { $BindHost } + $url = "http://${pollHost}:$Port/" + $ready = $false + for ($i = 0; $i -lt 240; $i++) { + if ($proc.HasExited) { Fail 'server exited during startup' } + # -s without -S: stderr output under redirection throws NativeCommandError on PS 5.1 + & curl.exe -fs "${url}health" > $null + if ($LASTEXITCODE -eq 0) { $ready = $true; break } + Start-Sleep -Milliseconds 500 + } + if (-not $ready) { Fail "server did not become ready at $url" } + + Write-Host "zonos2: ready -> $url" + if (-not $NoBrowser) { Start-Process $url } + + Write-Host 'Press Ctrl-C (or close this window) to stop the server.' + Wait-Process -Id $proc.Id +} finally { + if (-not $proc.HasExited) { Stop-Process -Id $proc.Id -Force -ErrorAction SilentlyContinue } +} diff --git a/scripts/start-zonos2.sh b/scripts/start-zonos2.sh new file mode 100755 index 0000000..de6f7fa --- /dev/null +++ b/scripts/start-zonos2.sh @@ -0,0 +1,217 @@ +#!/usr/bin/env bash +# start-zonos2 — download the models if missing, launch zonos2-server, open the browser. +# Double-click friendly: on macOS this file also ships as start-zonos2.command. +# Must stay bash-3.2 compatible (stock macOS bash). +set -euo pipefail + +SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" + +GPU_DEFAULT=cpu # CI stamps this to "gpu" in the vulkan/metal release archives +ZONOS2_QUANT="${ZONOS2_QUANT:-q6_k}" +ZONOS2_MODEL_DIR="${ZONOS2_MODEL_DIR:-$SCRIPT_DIR/models}" +ZONOS2_BASE_URL="${ZONOS2_BASE_URL:-https://huggingface.co/Zyphra/ZONOS2-GGUF/resolve/main}" +ZONOS2_HOST="${ZONOS2_HOST:-127.0.0.1}" +ZONOS2_PORT="${ZONOS2_PORT:-1919}" +ZONOS2_SERVER_BIN="${ZONOS2_SERVER_BIN:-$SCRIPT_DIR/zonos2-server}" +ASSUME_YES="${ZONOS2_ASSUME_YES:-0}" +NO_BROWSER="${ZONOS2_NO_BROWSER:-0}" +FFMPEG_URL="${ZONOS2_FFMPEG_URL:-}" # override with a single-binary URL (used by tests); default is per-OS +FFMPEG_DIR="$ZONOS2_MODEL_DIR/bin" +FFMPEG_BIN="$FFMPEG_DIR/ffmpeg" +GPU_MODE="$GPU_DEFAULT" +EXTRA_ARGS=() + +usage() { + cat <] + --quant Q model quant to fetch/serve (q4_k 4.9GB, q5_k 5.8GB, q6_k 6.8GB, + q8_0 8.5GB, f16 15.3GB; default $ZONOS2_QUANT) + --cpu | --gpu backend (archive default: $GPU_DEFAULT) + --host H bind address (default $ZONOS2_HOST) + --port P port (default $ZONOS2_PORT) + -y, --yes don't ask before downloading + --no-browser don't open the web UI +env overrides: ZONOS2_QUANT ZONOS2_MODEL_DIR ZONOS2_BASE_URL ZONOS2_HOST ZONOS2_PORT + ZONOS2_SERVER_BIN ZONOS2_ASSUME_YES ZONOS2_NO_BROWSER ZONOS2_FFMPEG_URL +ffmpeg (voice cloning only) is fetched into $ZONOS2_MODEL_DIR/bin if not already there or +on PATH; basic TTS needs no ffmpeg. +EOF +} + +die() { echo "start-zonos2: error: $*" >&2; exit 1; } + +while [ $# -gt 0 ]; do + case "$1" in + --quant) [ $# -ge 2 ] || die "--quant needs a value"; ZONOS2_QUANT="$2"; shift 2 ;; + --cpu) GPU_MODE=cpu; shift ;; + --gpu) GPU_MODE=gpu; shift ;; + --host) [ $# -ge 2 ] || die "--host needs a value"; ZONOS2_HOST="$2"; shift 2 ;; + --port) [ $# -ge 2 ] || die "--port needs a value"; ZONOS2_PORT="$2"; shift 2 ;; + -y|--yes) ASSUME_YES=1; shift ;; + --no-browser) NO_BROWSER=1; shift ;; + -h|--help) usage; exit 0 ;; + --) shift; EXTRA_ARGS=("$@"); break ;; + *) usage; die "unknown option '$1' (pass server flags after --)" ;; + esac +done + +[ -x "$ZONOS2_SERVER_BIN" ] || die "zonos2-server not found at $ZONOS2_SERVER_BIN (run this script from the extracted release archive, or set ZONOS2_SERVER_BIN)" + +model_size_gb() { + case "$1" in + zonos2-f16.gguf) echo 15.3 ;; + zonos2-q8_0.gguf) echo 8.5 ;; + zonos2-q6_k.gguf) echo 6.8 ;; + zonos2-q5_k.gguf) echo 5.8 ;; + zonos2-q4_k.gguf) echo 4.9 ;; + dac.gguf) echo 0.25 ;; + spk-encoder.gguf) echo 0.02 ;; + *) echo "?" ;; + esac +} + +# curl -C - --fail exits 33 when the .part is already complete (HTTP 416): restart clean once. +download_one() { + name="$1" + url="$ZONOS2_BASE_URL/$name" + dst="$ZONOS2_MODEL_DIR/$name" + echo "downloading $name ($(model_size_gb "$name") GB) ..." + rc=0 + curl -L --fail --retry 3 -C - --progress-bar -o "$dst.part" "$url" || rc=$? + if [ "$rc" = 33 ]; then + rm -f "$dst.part" + rc=0 + curl -L --fail --retry 3 --progress-bar -o "$dst.part" "$url" || rc=$? + fi + [ "$rc" = 0 ] || return "$rc" + mv "$dst.part" "$dst" +} + +ffmpeg_size_mb() { case "$(uname -s)" in Darwin) echo 45 ;; *) echo 108 ;; esac; } + +# Fetch a static ffmpeg into $FFMPEG_DIR — only needed for voice cloning (decoding the +# reference audio); basic TTS never touches it. Linux uses the BtbN LGPL build; macOS a +# pinned static arm64 binary (GPL, but we don't redistribute it — your machine fetches it +# from the provider, like the models from HF). Override with ZONOS2_FFMPEG_URL. +ffmpeg_download() { + mkdir -p "$FFMPEG_DIR" || return 1 + ff_url=""; ff_single=1 + if [ -n "$FFMPEG_URL" ]; then + ff_url="$FFMPEG_URL" + elif [ "$(uname -s)" = Darwin ]; then + ff_url="https://github.com/eugeneware/ffmpeg-static/releases/download/b6.1.1/ffmpeg-darwin-arm64" + else + ff_url="https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-n7.1-latest-linux64-lgpl-7.1.tar.xz" + ff_single=0 + fi + echo "downloading ffmpeg ($(ffmpeg_size_mb) MB, for voice cloning) ..." + if [ "$ff_single" = 1 ]; then + curl -L --fail --retry 3 --progress-bar -o "$FFMPEG_BIN.part" "$ff_url" || return 1 + chmod +x "$FFMPEG_BIN.part"; mv "$FFMPEG_BIN.part" "$FFMPEG_BIN" + else + # BtbN archive nests the binary at ffmpeg-*/bin/ffmpeg; extract, relocate, clean up + ff_tmp="$FFMPEG_DIR/ffmpeg-dl.tar.xz"; ff_x="$FFMPEG_DIR/.extract" + rm -rf "$ff_x"; mkdir -p "$ff_x" + curl -L --fail --retry 3 --progress-bar -o "$ff_tmp" "$ff_url" || { rm -rf "$ff_tmp" "$ff_x"; return 1; } + tar -xf "$ff_tmp" -C "$ff_x" || { rm -rf "$ff_tmp" "$ff_x"; return 1; } + ff_found=$(find "$ff_x" -type f -name ffmpeg -path '*/bin/*' 2>/dev/null | head -1) + [ -n "$ff_found" ] || { rm -rf "$ff_tmp" "$ff_x"; return 1; } + mv "$ff_found" "$FFMPEG_BIN"; chmod +x "$FFMPEG_BIN" + rm -rf "$ff_tmp" "$ff_x" + fi + [ -x "$FFMPEG_BIN" ] +} + +BACKBONE="zonos2-$ZONOS2_QUANT.gguf" +MISSING=() +for f in "$BACKBONE" dac.gguf spk-encoder.gguf; do + [ -f "$ZONOS2_MODEL_DIR/$f" ] || MISSING+=("$f") +done + +# ffmpeg is only needed for voice cloning. Prefer our own downloaded copy, then a system +# ffmpeg on PATH; otherwise mark it for download alongside the models. +NEED_FFMPEG=0 +if [ -x "$FFMPEG_BIN" ]; then + export ZONOS2_FFMPEG="$FFMPEG_BIN" +elif [ "${ZONOS2_SKIP_PATH_FFMPEG:-0}" != 1 ] && command -v ffmpeg >/dev/null 2>&1; then + : # a system ffmpeg is on PATH; the server/CLI resolve it there (ZONOS2_SKIP_PATH_FFMPEG=1 forces a download, for tests) +else + NEED_FFMPEG=1 +fi + +if [ "${#MISSING[@]}" -gt 0 ] || [ "$NEED_FFMPEG" = 1 ]; then + command -v curl >/dev/null 2>&1 || die "curl is required to download models — install it (e.g. 'sudo apt install curl' / 'brew install curl'), or place the .gguf files in $ZONOS2_MODEL_DIR yourself (see README)" + echo "The following will be downloaded into $ZONOS2_MODEL_DIR:" + for f in "${MISSING[@]}"; do + echo " $f ($(model_size_gb "$f") GB) from $ZONOS2_BASE_URL" + done + [ "$NEED_FFMPEG" = 1 ] && echo " ffmpeg (~$(ffmpeg_size_mb) MB, voice cloning only)" + if [ "$ASSUME_YES" != 1 ] && [ -t 0 ]; then + printf "Download now? [Y/n] " + read -r ans + case "$ans" in ""|y*|Y*) ;; *) echo "aborted."; exit 1 ;; esac + fi + mkdir -p "$ZONOS2_MODEL_DIR" || die "cannot create $ZONOS2_MODEL_DIR (set ZONOS2_MODEL_DIR to a writable location)" + for f in "${MISSING[@]}"; do + if ! download_one "$f"; then + if [ "$f" = spk-encoder.gguf ]; then + echo "start-zonos2: warning: failed to download $f — continuing without voice-clone upload support" >&2 + else + die "failed to download $f from $ZONOS2_BASE_URL/$f" + fi + fi + done + if [ "$NEED_FFMPEG" = 1 ]; then + if ffmpeg_download; then + export ZONOS2_FFMPEG="$FFMPEG_BIN" + else + echo "start-zonos2: warning: ffmpeg download failed — voice cloning disabled (basic TTS still works)" >&2 + fi + fi +fi + +# The UI, default voices, and emotion directions ship next to this script in release +# archives; in a source checkout the script lives in scripts/ with them one level up. +# Pass them explicitly because the server defaults are cwd-relative and a +# double-clicked .command runs from $HOME. +sibling() { # $1 = name: echo the archive-layout path, falling back to the repo layout + if [ -e "$SCRIPT_DIR/$1" ]; then echo "$SCRIPT_DIR/$1"; else echo "$SCRIPT_DIR/../$1"; fi +} + +args=("$ZONOS2_MODEL_DIR/$BACKBONE" + --dac "$ZONOS2_MODEL_DIR/dac.gguf" + --host "$ZONOS2_HOST" --port "$ZONOS2_PORT" + --ui "$(sibling web/tts_ui.html)") +[ -d "$(sibling default_voices)" ] && args+=(--tts-default-voices-dir "$(sibling default_voices)") +[ -d "$(sibling emotion_directions)" ] && args+=(--tts-emotion-directions-dir "$(sibling emotion_directions)") +[ -f "$ZONOS2_MODEL_DIR/spk-encoder.gguf" ] && args+=(--spk "$ZONOS2_MODEL_DIR/spk-encoder.gguf") +[ "$GPU_MODE" = gpu ] && args+=(--gpu) + +"$ZONOS2_SERVER_BIN" "${args[@]}" ${EXTRA_ARGS[@]+"${EXTRA_ARGS[@]}"} & +SRV_PID=$! +trap 'kill "$SRV_PID" 2>/dev/null || true' INT TERM EXIT + +# Poll readiness; bail out early if the server died (its error is already on the terminal). +POLL_HOST="$ZONOS2_HOST" +case "$POLL_HOST" in 0.0.0.0|::) POLL_HOST=127.0.0.1 ;; esac +URL="http://$POLL_HOST:$ZONOS2_PORT/" +ready=0 +i=0 +while [ $i -lt 240 ]; do + kill -0 "$SRV_PID" 2>/dev/null || die "server exited during startup" + if curl -fsS "${URL}health" >/dev/null 2>&1; then ready=1; break; fi + sleep 0.5 + i=$((i + 1)) +done +[ "$ready" = 1 ] || die "server did not become ready at $URL" + +echo "zonos2: ready -> $URL" +if [ "$NO_BROWSER" != 1 ]; then + case "$(uname -s)" in + Darwin) open "$URL" 2>/dev/null || true ;; + *) command -v xdg-open >/dev/null 2>&1 && xdg-open "$URL" 2>/dev/null || true ;; + esac +fi + +echo "Press Ctrl-C (or close this window) to stop the server." +wait "$SRV_PID" diff --git a/src/app-pages.h b/src/app-pages.h new file mode 100644 index 0000000..1d646a9 --- /dev/null +++ b/src/app-pages.h @@ -0,0 +1,117 @@ +// Built-in pages for zonos2-app, shown via saucer's webview->set_html() before the +// embedded server is up (setup / loading / error). Self-contained HTML: no network, +// no external assets. The pages call C++ through the functions app.cpp exposes on +// the smartview (`saucer.exposed.*`). ZONOS2_APP_ERROR_PAGE contains a %DETAILS% +// placeholder replaced (HTML-escaped) by app.cpp. +#pragma once + +static const char * ZONOS2_APP_STYLE = R"html( + +)html"; + +// ---------------------------------------------------------------- setup (first run) + +static const char * ZONOS2_APP_SETUP_PAGE = R"html( +
+

Zonos2

+

Pick the model files to use. They are saved for the next launch.

+ + +
+
+ + +
+
+ + +
+
+ +
+ +
+ +
+ +)html"; + +// ---------------------------------------------------------------- loading splash + +static const char * ZONOS2_APP_SPLASH_PAGE = R"html( +
+
+

Loading models…

+

This can take a little while on first launch.

+
+)html"; + +// ---------------------------------------------------------------- startup error + +static const char * ZONOS2_APP_ERROR_PAGE = R"html( +
+

Failed to start

+

The embedded zonos2 server did not come up.

+
%DETAILS%
+ +
+)html"; diff --git a/src/app.cpp b/src/app.cpp new file mode 100644 index 0000000..1f26503 --- /dev/null +++ b/src/app.cpp @@ -0,0 +1,443 @@ +// zonos2-app — native desktop shell around zonos2-server (saucer webview). +// +// The whole HTTP server runs in-process on a background thread (src/server.cpp via +// src/server-embed.h) on an ephemeral 127.0.0.1 port; the window is a platform +// webview (WebView2 / WKWebView / WebKitGTK) pointed at it, so the app UI is the +// exact same web/tts_ui.html the server serves to a browser — and the HTTP API +// stays reachable while the app runs. +// +// Flow: with server-style CLI args they pass through verbatim (power users). +// Otherwise a saved config (app.json in the platform config dir) boots straight +// into a loading splash -> UI; missing/invalid config (or --setup) shows a setup +// page with native .gguf pickers first. Closing the window stops the server. +// +// Built only with -DZONOS2_APP=ON (needs a C++23 toolchain; see README). +#include "server-embed.h" +#include "app-pages.h" +#include "exe-path.h" + +#include "httplib.h" +#include "json.hpp" + +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +namespace fs = std::filesystem; +using json = nlohmann::json; + +// --------------------------------------------------------------------------- paths + +static fs::path exe_dir() { return zonos2_exe_dir(); } + +static fs::path config_path() { +#ifdef _WIN32 + const char * base = getenv("APPDATA"); + if (base) return fs::path(base) / "zonos2" / "app.json"; +#elif defined(__APPLE__) + const char * home = getenv("HOME"); + if (home) return fs::path(home) / "Library" / "Application Support" / "zonos2" / "app.json"; +#else + const char * xdg = getenv("XDG_CONFIG_HOME"); + if (xdg && *xdg) return fs::path(xdg) / "zonos2" / "app.json"; + const char * home = getenv("HOME"); + if (home) return fs::path(home) / ".config" / "zonos2" / "app.json"; +#endif + return fs::path("zonos2-app.json"); // last resort: CWD +} + +// The web UI file, resolved relative to the executable (a double-clicked app has an +// arbitrary CWD): dist layout (web/ next to the binary), macOS bundle Resources, +// repo layout when running from a build tree, then the server's CWD-relative default. +static std::string resolve_ui_path() { + const fs::path dir = exe_dir(); + const fs::path cands[] = { + dir / "web" / "tts_ui.html", + dir / ".." / "Resources" / "tts_ui.html", + dir / ".." / "web" / "tts_ui.html", + }; + std::error_code ec; + for (const auto & c : cands) + if (fs::exists(c, ec)) return c.lexically_normal().string(); + return "web/tts_ui.html"; +} + +// --------------------------------------------------------------------------- config + +static json load_config() { + std::ifstream f(config_path()); + if (!f) return json::object(); + json j = json::parse(f, nullptr, false); + return j.is_object() ? j : json::object(); +} + +static std::string save_config(const json & j) { // returns error message or "" + const fs::path p = config_path(); + std::error_code ec; + fs::create_directories(p.parent_path(), ec); + std::ofstream f(p); + if (!f) return "cannot write " + p.string(); + f << j.dump(2) << "\n"; + return f.good() ? "" : "cannot write " + p.string(); +} + +static bool config_launchable(const json & j) { + std::error_code ec; + return j.contains("model") && j.contains("dac") && + fs::exists(j.value("model", ""), ec) && fs::exists(j.value("dac", ""), ec); +} + +// --------------------------------------------------------------------------- ffmpeg +// Voice cloning decodes the reference audio through ffmpeg; plain TTS never touches it. +// The packaged app can't assume ffmpeg is installed, so if none is found we fetch a +// static build into the app config dir (bin/) and point the embedded server at it via +// ZONOS2_FFMPEG. The fetch runs on a detached background thread so startup isn't blocked; +// cloning just fails gracefully (existing "ffmpeg failed" error) until it lands. + +static void set_env(const char * k, const std::string & v) { +#ifdef _WIN32 + _putenv_s(k, v.c_str()); +#else + setenv(k, v.c_str(), 1); +#endif +} + +static std::string shq(const std::string & s) { // quote a path for the system() shell +#ifdef _WIN32 + return "\"" + s + "\""; +#else + std::string q = "'"; + for (char c : s) q += (c == '\'') ? "'\\''" : std::string(1, c); + return q + "'"; +#endif +} + +static fs::path ffmpeg_bin_path() { +#ifdef _WIN32 + return config_path().parent_path() / "bin" / "ffmpeg.exe"; +#else + return config_path().parent_path() / "bin" / "ffmpeg"; +#endif +} + +static bool ffmpeg_on_path() { + const char * p = getenv("PATH"); + if (!p) return false; +#ifdef _WIN32 + const char sep = ';'; const char * name = "ffmpeg.exe"; +#else + const char sep = ':'; const char * name = "ffmpeg"; +#endif + std::error_code ec; + std::stringstream ss{std::string(p)}; + for (std::string dir; std::getline(ss, dir, sep); ) + if (!dir.empty() && fs::exists(fs::path(dir) / name, ec)) return true; + return false; +} + +// Pull the ffmpeg binary out of a BtbN archive (nested at ffmpeg-*/bin/ffmpeg[.exe]). +static bool extract_ffmpeg(const fs::path & archive, const fs::path & dst) { + std::error_code ec; + const fs::path tmp = dst.parent_path() / ".ffx"; + fs::remove_all(tmp, ec); fs::create_directories(tmp, ec); + // tar handles .tar.xz (GNU tar) and .zip (bsdtar ships on Win10 1803+, like curl.exe) + if (std::system(("tar -xf " + shq(archive.string()) + " -C " + shq(tmp.string())).c_str()) != 0) { + fs::remove_all(tmp, ec); return false; + } + fs::path found; + for (fs::recursive_directory_iterator it(tmp, ec), end; !ec && it != end; it.increment(ec)) + if (it->is_regular_file(ec) && it->path().filename() == dst.filename() && + it->path().parent_path().filename() == "bin") { found = it->path(); break; } + bool ok = false; + if (!found.empty()) { + fs::rename(found, dst, ec); + if (ec) { ec.clear(); fs::copy_file(found, dst, fs::copy_options::overwrite_existing, ec); } + ok = !ec; + } + fs::remove_all(tmp, ec); + return ok; +} + +static bool download_ffmpeg(const fs::path & dst) { + std::error_code ec; + fs::create_directories(dst.parent_path(), ec); + + std::string url; bool single; + if (const char * o = getenv("ZONOS2_FFMPEG_URL"); o && *o) { url = o; single = true; } // test override +#if defined(__APPLE__) + else { url = "https://github.com/eugeneware/ffmpeg-static/releases/download/b6.1.1/ffmpeg-darwin-arm64"; single = true; } +#elif defined(_WIN32) + else { url = "https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-n7.1-latest-win64-lgpl-7.1.zip"; single = false; } +#else + else { url = "https://github.com/BtbN/FFmpeg-Builds/releases/download/latest/ffmpeg-n7.1-latest-linux64-lgpl-7.1.tar.xz"; single = false; } +#endif + + if (single) { + const fs::path part = fs::path(dst.string() + ".part"); + if (std::system(("curl -L --fail --retry 3 -o " + shq(part.string()) + " " + shq(url)).c_str()) != 0) return false; + fs::rename(part, dst, ec); + if (ec) return false; + } else { + const fs::path arc = dst.parent_path() / "ffmpeg-dl.archive"; + if (std::system(("curl -L --fail --retry 3 -o " + shq(arc.string()) + " " + shq(url)).c_str()) != 0) return false; + const bool ok = extract_ffmpeg(arc, dst); + fs::remove(arc, ec); + if (!ok) return false; + } +#ifndef _WIN32 + fs::permissions(dst, fs::perms::owner_all | fs::perms::group_read | fs::perms::group_exec | + fs::perms::others_read | fs::perms::others_exec, ec); +#endif + return fs::exists(dst, ec); +} + +// Ensure ffmpeg is available and, when we own it, point the server at it via ZONOS2_FFMPEG. +// Call once, before the embedded server starts (sets the env single-threaded); the actual +// fetch is detached. +static void ensure_ffmpeg() { + if (const char * e = getenv("ZONOS2_FFMPEG"); e && *e) return; // user/CLI override wins + const fs::path bin = ffmpeg_bin_path(); + std::error_code ec; + if (fs::exists(bin, ec)) { set_env("ZONOS2_FFMPEG", bin.string()); return; } + if (getenv("ZONOS2_SKIP_PATH_FFMPEG") == nullptr && ffmpeg_on_path()) return; // use system ffmpeg + // none present: reserve the path now (env set before any server thread), fetch in background + set_env("ZONOS2_FFMPEG", bin.string()); + std::thread([bin] { + if (!download_ffmpeg(bin)) + fprintf(stderr, "zonos2-app: ffmpeg download failed — voice cloning disabled until it is available\n"); + else + fprintf(stderr, "zonos2-app: ffmpeg ready at %s\n", bin.string().c_str()); + }).detach(); +} + +// --------------------------------------------------------------------------- state + +struct app_state { + zonos2_server_ctl ctl; + std::thread server_th, watcher_th; + std::vector args; // owns the server's argv strings + std::vector cargs; + std::atomic server_done{false}; + std::atomic server_rc{0}; + std::atomic shutdown{false}; // app closing: watcher must bail out +}; + +static app_state g_state; +static std::vector g_passthrough; // server args given on our command line +static bool g_force_setup = false; + +// --------------------------------------------------------------------------- pages + +static std::string page(const std::string & body) { + return std::string("") + + ZONOS2_APP_STYLE + "" + body + ""; +} + +static std::string html_escape(const std::string & s) { + std::string out; + out.reserve(s.size()); + for (char c : s) switch (c) { + case '&': out += "&"; break; + case '<': out += "<"; break; + case '>': out += ">"; break; + default: out += c; + } + return out; +} + +static std::string error_page(const std::string & details) { + std::string body = ZONOS2_APP_ERROR_PAGE; + const std::string ph = "%DETAILS%"; + if (auto pos = body.find(ph); pos != std::string::npos) + body.replace(pos, ph.size(), html_escape(details)); + return page(body); +} + +// --------------------------------------------------------------------------- server lifecycle + +static void stop_server() { + if (auto * svr = g_state.ctl.svr.load()) svr->stop(); + if (g_state.server_th.joinable()) g_state.server_th.join(); + if (g_state.watcher_th.joinable()) g_state.watcher_th.join(); +} + +// Waits for the server thread to bind, then for /health, then swaps the webview to +// the served UI. Runs on its own thread; saucer's set_url/set_html marshal to the +// app thread internally. The smartview outlives these threads: they are joined in +// start()'s coroutine frame, which owns it. +static void watch_server(saucer::smartview * webview) { + using clock = std::chrono::steady_clock; + const auto deadline = clock::now() + std::chrono::minutes(10); // model load can be slow on cold disks + + auto fail = [&](const std::string & why) { + webview->set_html(error_page( + why + "\n\nConfig: " + config_path().string() + + "\nTip: run zonos2-app from a terminal to see the full server log.")); + }; + + int port = 0; + while (!g_state.shutdown.load()) { + port = g_state.ctl.port.load(); + if (port > 0) break; + if (g_state.ctl.failed.load() || g_state.server_done.load()) + return fail("server exited during startup (code " + std::to_string(g_state.server_rc.load()) + ")"); + if (clock::now() > deadline) return fail("timed out waiting for the server to start"); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + } + + httplib::Client cli("127.0.0.1", port); + cli.set_connection_timeout(2); + cli.set_read_timeout(2); + while (!g_state.shutdown.load()) { + if (auto res = cli.Get("/health"); res && res->status == 200) { + webview->set_url("http://127.0.0.1:" + std::to_string(port) + "/"); + return; + } + if (g_state.ctl.failed.load() || g_state.server_done.load()) + return fail("server exited during startup (code " + std::to_string(g_state.server_rc.load()) + ")"); + if (clock::now() > deadline) return fail("timed out waiting for the server to become healthy"); + std::this_thread::sleep_for(std::chrono::milliseconds(200)); + } +} + +static void start_server(saucer::smartview * webview, std::vector args) { + stop_server(); // joins any previous (failed) attempt + g_state.ctl.port.store(0); + g_state.ctl.failed.store(false); + g_state.server_done.store(false); + + g_state.args = std::move(args); + g_state.cargs.clear(); + for (auto & a : g_state.args) g_state.cargs.push_back(a.data()); + + g_state.server_th = std::thread([] { + const int rc = zonos2_server_main((int) g_state.cargs.size(), g_state.cargs.data(), &g_state.ctl); + g_state.server_rc.store(rc); + g_state.server_done.store(true); + }); + g_state.watcher_th = std::thread([webview] { watch_server(webview); }); +} + +static std::vector args_from_config(const json & cfg) { + std::vector a = {"zonos2-app", cfg.value("model", ""), "--dac", cfg.value("dac", "")}; + if (const std::string spk = cfg.value("spk", ""); !spk.empty()) { a.push_back("--spk"); a.push_back(spk); } + a.push_back(cfg.value("gpu", true) ? "--gpu" : "--cpu"); + a.insert(a.end(), {"--host", "127.0.0.1", "--port", "0", "--ui", resolve_ui_path()}); + for (const auto & e : cfg.value("extra_args", std::vector{})) a.push_back(e); + return a; +} + +// --------------------------------------------------------------------------- saucer app + +coco::stray start(saucer::application * app) { + auto window = saucer::window::create(app).value(); + auto webview_ = saucer::smartview::create({.window = window}); + if (!webview_) { + fprintf(stderr, "zonos2-app: failed to create the webview\n"); + app->quit(); + co_return; + } + saucer::smartview * webview = &*webview_; + auto desktop = saucer::modules::desktop{app}; + + window->set_title("Zonos2"); + window->set_size({.w = 1100, .h = 800}); + { + // Window icon where the platform supports it (no-op on WebKitGTK; the + // Windows taskbar/exe icon comes from the .rc resource, macOS from the bundle). + std::error_code ec; + for (const fs::path c : {exe_dir() / "zonos2.png", exe_dir() / ".." / "Resources" / "zonos2.png"}) + if (fs::exists(c, ec)) { + if (auto ic = saucer::icon::from(c)) window->set_icon(*ic); + break; + } + } + + webview->expose("pick_file", [&desktop](const std::string & /*kind*/) -> std::string { + auto r = desktop.pick({.filters = {"*.gguf"}}); + return r ? r->string() : std::string{}; + }); + + webview->expose("get_config", []() -> std::string { return load_config().dump(); }); + + webview->expose("launch", [webview](const std::string & model, const std::string & dac, + const std::string & spk, bool gpu) -> std::string { + std::error_code ec; + if (!fs::exists(model, ec)) return "backbone model not found: " + model; + if (!fs::exists(dac, ec)) return "DAC decoder not found: " + dac; + if (!spk.empty() && !fs::exists(spk, ec)) return "speaker encoder not found: " + spk; + + json cfg = load_config(); + cfg["model"] = model; cfg["dac"] = dac; cfg["spk"] = spk; cfg["gpu"] = gpu; + if (auto err = save_config(cfg); !err.empty()) + fprintf(stderr, "zonos2-app: warning: %s\n", err.c_str()); + + webview->set_html(page(ZONOS2_APP_SPLASH_PAGE)); + start_server(webview, args_from_config(cfg)); + return ""; + }); + + webview->expose("show_setup", [webview] { webview->set_html(page(ZONOS2_APP_SETUP_PAGE)); }); + + if (!g_passthrough.empty()) { + webview->set_html(page(ZONOS2_APP_SPLASH_PAGE)); + start_server(webview, g_passthrough); + } else if (const json cfg = load_config(); !g_force_setup && config_launchable(cfg)) { + webview->set_html(page(ZONOS2_APP_SPLASH_PAGE)); + start_server(webview, args_from_config(cfg)); + } else { + webview->set_html(page(ZONOS2_APP_SETUP_PAGE)); + } + + window->show(); + co_await app->finish(); + + g_state.shutdown.store(true); + stop_server(); +} + +static void usage() { + printf("usage: zonos2-app [--setup] | [ --dac [server options]]\n" + "\n" + "Native desktop app for the zonos2 TTS server.\n" + "\n" + " (no arguments) launch with the saved config; first run opens a setup page\n" + " to pick the .gguf model files (config: %s)\n" + " --setup reopen the setup page\n" + " --help this help\n" + "\n" + "Any other arguments are passed through verbatim to the embedded zonos2-server\n" + "(run `zonos2-server` for its options); the UI then attaches to whatever it binds.\n", + config_path().string().c_str()); +} + +int main(int argc, char ** argv) { + for (int i = 1; i < argc; ++i) { + const std::string a = argv[i]; + if (a == "--help" || a == "-h") { usage(); return 0; } // before any GUI init (headless-safe) + if (a == "--setup") { g_force_setup = true; continue; } + g_passthrough.push_back(a); + } + if (!g_passthrough.empty()) g_passthrough.insert(g_passthrough.begin(), "zonos2-app"); + + ensure_ffmpeg(); // fetch ffmpeg for voice cloning in the background if none is present + + // Deliberately not forwarding argc/argv: GTK would try to parse the + // server-passthrough options (--dac, ...) and error out on them. + auto app = saucer::application::create({.id = "zonos2"}); + if (!app) { + fprintf(stderr, "zonos2-app: failed to initialize the GUI: %s\n", app.error().message().c_str()); + return 1; + } + return app->run(start); +} diff --git a/src/exe-path.h b/src/exe-path.h new file mode 100644 index 0000000..452a8bb --- /dev/null +++ b/src/exe-path.h @@ -0,0 +1,47 @@ +// exe-path.h — locate the running executable's directory, portably. Used to resolve +// assets and helper binaries (web UI, a bundled/downloaded ffmpeg) relative to the +// binary rather than the process CWD, which is arbitrary for a double-clicked app. +#pragma once + +#include +#include +#include + +#ifdef _WIN32 +// This header pulls , and translation units that also use httplib (app.cpp) +// include it before 's . Left alone, drags in the +// legacy (v1), which then hard-conflicts with . Block just +// winsock1 via _WINSOCKAPI_ (not WIN32_LEAN_AND_MEAN, which would also strip the OLE/COM +// headers saucer's WebView2 backend needs) and match httplib's NOMINMAX. +#ifndef _WINSOCKAPI_ +#define _WINSOCKAPI_ +#endif +#ifndef NOMINMAX +#define NOMINMAX +#endif +#include +#elif defined(__APPLE__) +#include +#endif + +static inline std::filesystem::path zonos2_exe_dir() { + namespace fs = std::filesystem; + std::error_code ec; +#ifdef _WIN32 + wchar_t buf[MAX_PATH]; + const DWORD n = GetModuleFileNameW(nullptr, buf, MAX_PATH); + if (n > 0 && n < MAX_PATH) return fs::path(std::wstring(buf, n)).parent_path(); +#elif defined(__APPLE__) + uint32_t sz = 0; + _NSGetExecutablePath(nullptr, &sz); + std::string b(sz, '\0'); + if (_NSGetExecutablePath(b.data(), &sz) == 0) { + fs::path p = fs::canonical(b.c_str(), ec); + if (!ec) return p.parent_path(); + } +#else + fs::path p = fs::canonical("/proc/self/exe", ec); + if (!ec) return p.parent_path(); +#endif + return fs::current_path(ec); +} diff --git a/src/main.cpp b/src/main.cpp index 9adda56..b12caae 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,6 +6,9 @@ #include "spk-encoder.h" #include "npy.h" #include "ggml.h" +#include "prune-stats.h" +#include "prune-policy.h" +#include "model-paths.h" #include #include @@ -24,11 +27,13 @@ static void usage(const char * a0) { " %s --build-prompt \"\" [--speaker ]\n" " %s --batch-test \"\" [--slots N] [--max N] [--greedy] [--dac ] --gpu\n" " (with --dac, a .wav output is decoded directly; an .npy output also writes a sibling .wav)\n" + " (--dac / --spk-encoder default to dac.gguf / spk-encoder.gguf next to when present)\n" " (--dump-ids on --generate/--tts writes the full teacher-forcing sequence for imatrix calibration)\n" + " (--prune-mask (--keep N | --mass-eps E | --drop-below-hits H): audition a pruned recipe by ear before baking with prune-cli)\n" " conditioning paths (--tts/--build-prompt): [--inaccurate] [--noisy-bg] [--speaking-rate N] [--quality f:b[,f:b...]]\n" " emotion control (--tts/--generate): [--emotion happy=1[,sad=-0.5]] [--emotion-valence X] [--emotion-arousal X]\n" " [--emotion-strength X] [--emotion-cfg-scale X] [--emotion-dir DIR]\n" - " voice cloning: --speaker (precomputed) OR --clone --spk-encoder \n" + " voice cloning: --speaker (precomputed) OR --clone --spk-encoder|--spk \n" " (--clone encodes the reference in-process via ffmpeg+ECAPA; add --save-speaker to cache it)\n", a0, a0, a0, a0, a0, a0); } @@ -51,7 +56,7 @@ static void emit_output(const std::string & out, const std::vector & co printf("wrote %s\n", out.c_str()); } if (want_wav && dac_path.empty()) { - fprintf(stderr, "error: .wav output requested but no --dac given\n"); + fprintf(stderr, "error: .wav output requested but no --dac given (also auto-detected next to )\n"); return; } if (!dac_path.empty() && n_frames > 0) { @@ -313,6 +318,9 @@ int main(int argc, char ** argv) { bool validate = false, generate = false, do_tts = false, do_build_prompt = false, do_batch_test = false, do_decode_bench = false; std::string ids_path, out_dir, out_codes, spk_path, text, prompt_out, dac_path, dump_ids_path; std::string clone_path, spk_encoder_path, save_spk_path; // one-command voice cloning + std::string prune_mask_path; // --prune-mask: runtime expert pruning + int mask_keep = -1, mask_drop_hits = -1; + double mask_mass_eps = -1.0; int n_layer_limit = -1, max_frames = 400, spk_pos = 0, n_slots = 1; bool use_kv = true; // conditioning-path overrides (tri-state: <0 = leave prompt-builder default) @@ -349,13 +357,17 @@ int main(int argc, char ** argv) { else if (!strcmp(argv[i], "--dac") && i + 1 < argc) dac_path = argv[++i]; else if (!strcmp(argv[i], "--speaker") && i + 1 < argc) spk_path = argv[++i]; else if (!strcmp(argv[i], "--clone") && i + 1 < argc) clone_path = argv[++i]; - else if (!strcmp(argv[i], "--spk-encoder") && i + 1 < argc) spk_encoder_path = argv[++i]; + else if ((!strcmp(argv[i], "--spk-encoder") || !strcmp(argv[i], "--spk")) && i + 1 < argc) spk_encoder_path = argv[++i]; else if (!strcmp(argv[i], "--save-speaker") && i + 1 < argc) save_spk_path = argv[++i]; else if (!strcmp(argv[i], "--speaker-pos") && i + 1 < argc) spk_pos = atoi(argv[++i]); else if (!strcmp(argv[i], "--seed") && i + 1 < argc) sp.seed = (uint32_t) atoi(argv[++i]); else if (!strcmp(argv[i], "--greedy")) sp.greedy = true; else if (!strcmp(argv[i], "--recompute")) use_kv = false; else if (!strcmp(argv[i], "--dump-ids") && i + 1 < argc) dump_ids_path = argv[++i]; + else if (!strcmp(argv[i], "--prune-mask") && i + 1 < argc) prune_mask_path = argv[++i]; + else if (!strcmp(argv[i], "--keep") && i + 1 < argc) mask_keep = atoi(argv[++i]); + else if (!strcmp(argv[i], "--drop-below-hits") && i + 1 < argc) mask_drop_hits = atoi(argv[++i]); + else if (!strcmp(argv[i], "--mass-eps") && i + 1 < argc) mask_mass_eps = atof(argv[++i]); else if (!strcmp(argv[i], "--inaccurate")) cond_inaccurate = 1; // accurate_mode = false else if (!strcmp(argv[i], "--noisy-bg")) cond_noisy_bg = 1; // clean_speaker_background = false else if (!strcmp(argv[i], "--speaking-rate") && i + 1 < argc) cond_rate = atoi(argv[++i]); @@ -371,12 +383,36 @@ int main(int argc, char ** argv) { else { usage(argv[0]); return 1; } } + if (!mp_file_exists(path)) { + fprintf(stderr, "failed to load model %s\n", path.c_str()); + mp_print_download_hint("zonos2-q6_k.gguf (or another quant)", path); + return 1; + } + + // Companion discovery, gated on the modes that actually need the file (an unconditional + // dac pickup would silently start emitting sibling .wavs on .npy outputs). + if (dac_path.empty() && !out_codes.empty() && ends_with(out_codes, ".wav")) { + dac_path = mp_find_companion(path, "dac.gguf"); + if (!dac_path.empty()) fprintf(stderr, "zonos2-cli: using dac.gguf found next to model: %s\n", dac_path.c_str()); + else { + // fail before generation rather than after minutes of decode + fprintf(stderr, "error: .wav output requested but no --dac given (also auto-detected next to )\n"); + mp_print_download_hint("dac.gguf", mp_parent_dir(path) + "/dac.gguf"); + return 1; + } + } + if (spk_encoder_path.empty() && !clone_path.empty()) { + spk_encoder_path = mp_find_companion(path, "spk-encoder.gguf"); + if (!spk_encoder_path.empty()) fprintf(stderr, "zonos2-cli: using spk-encoder.gguf found next to model: %s\n", spk_encoder_path.c_str()); + } + if (!clone_path.empty() && !spk_path.empty()) { fprintf(stderr, "error: --clone and --speaker are mutually exclusive (pick reference audio OR a precomputed embedding)\n"); return 1; } if (!clone_path.empty() && spk_encoder_path.empty()) { - fprintf(stderr, "error: --clone requires --spk-encoder \n"); + fprintf(stderr, "error: --clone requires --spk-encoder (also auto-detected next to )\n"); + mp_print_download_hint("spk-encoder.gguf", mp_parent_dir(path) + "/spk-encoder.gguf"); return 1; } if (clone_path.empty() && (!spk_encoder_path.empty() || !save_spk_path.empty())) { @@ -386,10 +422,24 @@ int main(int argc, char ** argv) { zonos2_model model; if (!zonos2_model_load(model, path.c_str(), use_gpu)) { - fprintf(stderr, "load failed\n"); + fprintf(stderr, "failed to load model %s\n", path.c_str()); + if (!mp_file_exists(path)) mp_print_download_hint("zonos2-q6_k.gguf (or another quant)", path); return 1; } + // --prune-mask: dynamically drop low-MSAN experts (runtime equivalent of prune-cli) so a + // pruned recipe can be auditioned by ear before baking it into a GGUF. + if (!prune_mask_path.empty()) { + if (mask_keep < 0 && mask_drop_hits < 0 && mask_mass_eps < 0.0) { + fprintf(stderr, "error: --prune-mask needs --keep N, --mass-eps E, or --drop-below-hits H\n"); + zonos2_model_free(model); return 1; + } + std::map st; + if (!prune_stats::load(prune_mask_path, st)) { zonos2_model_free(model); return 1; } + const auto keep = prune_policy::select(st, { mask_keep, mask_drop_hits, mask_mass_eps }); + if (!zonos2_set_expert_mask(model, keep)) { zonos2_model_free(model); return 1; } + } + // optional speaker embedding ([spk_dim] or [1, spk_dim] f32) for voice cloning: // either a precomputed --speaker npy, or --clone encoded in-process // through the ECAPA speaker encoder (one-command cloning). @@ -398,6 +448,7 @@ int main(int argc, char ** argv) { spk_model sm; if (!spk_load(sm, spk_encoder_path.c_str())) { fprintf(stderr, "failed to load speaker encoder %s\n", spk_encoder_path.c_str()); + if (!mp_file_exists(spk_encoder_path)) mp_print_download_hint("spk-encoder.gguf", spk_encoder_path); zonos2_model_free(model); return 1; } spk = spk_embed_from_file(sm, clone_path.c_str()); diff --git a/src/model-paths.h b/src/model-paths.h new file mode 100644 index 0000000..14b9022 --- /dev/null +++ b/src/model-paths.h @@ -0,0 +1,32 @@ +// model-paths.h — companion-GGUF discovery and download hints for the CLIs. +// dac.gguf / spk-encoder.gguf are conventionally siblings of the backbone GGUF; +// when the flags are omitted we look there before giving up. +#pragma once + +#include +#include + +static inline bool mp_file_exists(const std::string & p) { + if (FILE * f = fopen(p.c_str(), "rb")) { fclose(f); return true; } + return false; +} + +static inline std::string mp_parent_dir(const std::string & p) { + const size_t pos = p.find_last_of("/\\"); + return pos == std::string::npos ? std::string(".") : p.substr(0, pos); +} + +// Sibling of `backbone` named `name` if it exists, else "". +static inline std::string mp_find_companion(const std::string & backbone, const char * name) { + const std::string cand = mp_parent_dir(backbone) + "/" + name; + return mp_file_exists(cand) ? cand : std::string(); +} + +static inline void mp_print_download_hint(const char * name, const std::string & tried) { + fprintf(stderr, + "error: %s not found (tried: %s)\n" + "hint: download it with\n" + " curl -L -o %s https://huggingface.co/Zyphra/ZONOS2-GGUF/resolve/main/%s\n" + "or run the bundled start-zonos2 script, which fetches all models automatically.\n", + name, tried.c_str(), tried.c_str(), name); +} diff --git a/src/perplexity.cpp b/src/perplexity.cpp index 10f15ea..b21f2a5 100644 --- a/src/perplexity.cpp +++ b/src/perplexity.cpp @@ -12,6 +12,8 @@ #include "zonos2.h" #include "npy.h" #include "imatrix.h" +#include "prune-stats.h" +#include "prune-policy.h" #include #include @@ -46,9 +48,15 @@ void usage(const char * a0) { " %s --kl-divergence-base {... [--speaker s.npy] | --manifest m.txt} [--cpu|--gpu]\n" " %s --kl-divergence [--cpu|--gpu]\n" " %s --imatrix-out {... | --manifest m.txt} [--imatrix-min-hits N] [--cpu|--gpu]\n" + " %s --prune-stats {... | --manifest m.txt} [--cpu|--gpu]\n" + " (--imatrix-out and --prune-stats may be combined: one capture pass, two sidecar outputs)\n" + " (--prune-mask (--keep N | --drop-below-hits H): runtime expert pruning before any\n" + " mode above -- sweep the quality knee vs a --kl-divergence base without writing GGUFs)\n" + " (--skip-layers L1,L2,...: depth pruning -- bypass whole transformer blocks (attn+FFN) before\n" + " any mode above; sweep the block-redundancy knee vs a --kl-divergence base)\n" " (ids.npy: row-major [n, n_codebooks+1] input_ids, e.g. from `zonos2-cli --build-prompt`)\n" " (--manifest: per-line `ids.npy [speaker.npy [spk_pos]]` -- per-sequence conditioning for multi-speaker/multi-path bases)\n", - a0, a0, a0, a0); + a0, a0, a0, a0, a0); } // load a 2-D [n, W] input_ids npy; W must equal n_codebooks+1. @@ -371,7 +379,7 @@ int run_kl_divergence(const zonos2_model & m, const std::string & base_path) { // --imatrix-out : collect per-expert importance (mean activation^2) over the corpus // --------------------------------------------------------------------------- int run_imatrix(const zonos2_model & m, const std::vector & seqs, - const std::string & out_path, int min_hits) { + const std::string & out_path, const std::string & prune_path, int min_hits) { const int ne = (int) m.hp.n_expert; if (ne == 0) { fprintf(stderr, "imatrix: model has no experts\n"); return 1; } @@ -411,6 +419,7 @@ int run_imatrix(const zonos2_model & m, const std::vector & seqs, } std::map im; + std::map ps; // per-layer MSAN/hit counts for prune-cli int64_t total_hits = 0; int zero_slots = 0, below_thresh = 0, n_layers = 0; std::vector all_cnt; // every (layer,expert) hit count for (auto & kv : acc) { @@ -438,16 +447,33 @@ int run_imatrix(const zonos2_model & m, const std::vector & seqs, im[p + "ffn_gate_exps.weight"] = std::move(gate); im[p + "ffn_up_exps.weight"] = std::move(up); im[p + "ffn_down_exps.weight"] = std::move(down); + + if (!prune_path.empty()) { + // MSAN_e = mean over routed tokens of ‖down-input‖² = sum_c A.dn[e,c] / cnt_e. + // Raw cnt (not the min-hits floor) so rarely-routed experts get their true — and + // typically lowest — score, which is exactly what flags them as prune candidates. + prune_stats::layer pl; + pl.top_k = (kv.first < (int) m.layers.size()) ? m.layers[kv.first].top_k : 0; + pl.cnt.assign(A.cnt.begin(), A.cnt.end()); + pl.msan.resize(ne); + for (int e = 0; e < ne; ++e) { + double s = 0.0; + for (int c = 0; c < A.n_ff; ++c) s += A.dn[(size_t) e*A.n_ff + c]; + pl.msan[e] = (float) (A.cnt[e] > 0 ? s / (double) A.cnt[e] : 0.0); + } + ps[kv.first] = std::move(pl); + } } - if (!imatrix::save(out_path, im)) return 1; + if (!out_path.empty() && !imatrix::save(out_path, im)) return 1; + if (!prune_path.empty() && !prune_stats::save(prune_path, ps)) return 1; std::sort(all_cnt.begin(), all_cnt.end()); const int64_t hmin = all_cnt.empty() ? 0 : all_cnt.front(); const int64_t hmed = all_cnt.empty() ? 0 : all_cnt[all_cnt.size() / 2]; const int64_t hmax = all_cnt.empty() ? 0 : all_cnt.back(); - printf("\n=== imatrix ===\n"); - printf("wrote %s : %d MoE layers, %d experts, %lld routed (token,expert) hits\n", - out_path.c_str(), n_layers, ne, (long long) total_hits); + printf("\n=== expert importance ===\n"); + printf("%d MoE layers, %d experts, %lld routed (token,expert) hits\n", + n_layers, ne, (long long) total_hits); printf("per-(layer,expert) hits: min %lld, median %lld, max %lld (min-hits floor = %d)\n", (long long) hmin, (long long) hmed, (long long) hmax, min_hits); if (zero_slots || below_thresh) @@ -455,6 +481,8 @@ int run_imatrix(const zonos2_model & m, const std::vector & seqs, zero_slots, below_thresh, (int) all_cnt.size(), (zero_slots + below_thresh) * 4 > (int) all_cnt.size() ? "consider more/longer generation traces" : "ok, well-covered"); + if (!out_path.empty()) printf("wrote imatrix %s\n", out_path.c_str()); + if (!prune_path.empty()) printf("wrote prune-stats %s\n", prune_path.c_str()); return 0; } @@ -466,10 +494,14 @@ int main(int argc, char ** argv) { enum { NONE, PPL, KLBASE, KLDIV, IMATRIX } mode = NONE; bool use_gpu = false; - std::string base_path, spk_path, manifest_path; + std::string base_path, prune_path, spk_path, manifest_path; std::vector ids_paths; int spk_pos = 0; int imat_min_hits = 32; // experts seen fewer than this many times fall back to RTN + std::string prune_mask_path; // --prune-mask: dynamically drop experts before this run + int mask_keep = -1, mask_drop_hits = -1, mask_drop_lowest = -1; + double mask_mass_eps = -1.0; + std::string skip_layers_arg; // --skip-layers L1,L2,...: bypass whole transformer blocks for (int i = 2; i < argc; ++i) { const char * a = argv[i]; @@ -479,7 +511,14 @@ int main(int argc, char ** argv) { else if (!strcmp(a, "--kl-divergence-base") && i + 1 < argc) { mode = KLBASE; base_path = argv[++i]; } else if (!strcmp(a, "--kl-divergence") && i + 1 < argc) { mode = KLDIV; base_path = argv[++i]; } else if (!strcmp(a, "--imatrix-out") && i + 1 < argc) { mode = IMATRIX; base_path = argv[++i]; } + else if (!strcmp(a, "--prune-stats") && i + 1 < argc) { mode = IMATRIX; prune_path = argv[++i]; } else if (!strcmp(a, "--imatrix-min-hits") && i + 1 < argc) imat_min_hits = atoi(argv[++i]); + else if (!strcmp(a, "--prune-mask") && i + 1 < argc) prune_mask_path = argv[++i]; + else if (!strcmp(a, "--keep") && i + 1 < argc) mask_keep = atoi(argv[++i]); + else if (!strcmp(a, "--drop-below-hits") && i + 1 < argc) mask_drop_hits = atoi(argv[++i]); + else if (!strcmp(a, "--mass-eps") && i + 1 < argc) mask_mass_eps = atof(argv[++i]); + else if (!strcmp(a, "--drop-lowest") && i + 1 < argc) mask_drop_lowest = atoi(argv[++i]); + else if (!strcmp(a, "--skip-layers") && i + 1 < argc) skip_layers_arg = argv[++i]; else if (!strcmp(a, "--manifest") && i + 1 < argc) manifest_path = argv[++i]; else if (!strcmp(a, "--speaker") && i + 1 < argc) spk_path = argv[++i]; else if (!strcmp(a, "--speaker-pos") && i + 1 < argc) spk_pos = atoi(argv[++i]); @@ -502,6 +541,43 @@ int main(int argc, char ** argv) { if (!zonos2_model_load(model, model_path.c_str(), use_gpu)) { fprintf(stderr, "load failed\n"); return 1; } + + // --prune-mask: dynamically drop low-MSAN experts before scoring (runtime equivalent of + // prune-cli, for sweeping the quality knee against a KLD base without rewriting GGUFs). + if (!prune_mask_path.empty()) { + if (mask_keep < 0 && mask_drop_hits < 0 && mask_mass_eps < 0.0 && mask_drop_lowest < 0) { + fprintf(stderr, "perplexity: --prune-mask needs --keep N, --mass-eps E, --drop-lowest N, or --drop-below-hits H\n"); + zonos2_model_free(model); return 1; + } + std::map st; + if (!prune_stats::load(prune_mask_path, st)) { zonos2_model_free(model); return 1; } + const auto keep = prune_policy::select(st, { mask_keep, mask_drop_hits, mask_mass_eps, mask_drop_lowest }); + if (!zonos2_set_expert_mask(model, keep)) { zonos2_model_free(model); return 1; } + } + + // --skip-layers: bypass whole transformer blocks (depth pruning) at graph-build time. Comma- + // separated block indices; the residual stream passes through each listed block unchanged. + if (!skip_layers_arg.empty()) { + model.layer_skip.assign(model.hp.n_layer, 0); + std::string ls; int n_skip = 0; + for (size_t p = 0; p <= skip_layers_arg.size(); ++p) { + const char c = p < skip_layers_arg.size() ? skip_layers_arg[p] : ','; + if (c == ',') { + if (!ls.empty()) { + const int L = atoi(ls.c_str()); + if (L < 0 || L >= (int) model.hp.n_layer) { + fprintf(stderr, "perplexity: --skip-layers index %d out of range [0,%u)\n", + L, model.hp.n_layer); + zonos2_model_free(model); return 1; + } + if (!model.layer_skip[L]) { model.layer_skip[L] = 1; ++n_skip; } + ls.clear(); + } + } else if (c != ' ') ls.push_back(c); + } + printf("depth-prune: skipping %d/%u blocks [%s]\n", n_skip, model.hp.n_layer, skip_layers_arg.c_str()); + } + const int W = (int) model.hp.n_codebooks + 1; // optional speaker embedding (kl-divergence reads it from the base file instead) @@ -523,7 +599,7 @@ int main(int argc, char ** argv) { std::vector seqs; if (load_manifest(manifest_path, W, model.hp.spk_dim, seqs)) { printf("manifest: %zu sequences from %s\n", seqs.size(), manifest_path.c_str()); - rc = (mode == IMATRIX) ? run_imatrix(model, seqs, base_path, imat_min_hits) + rc = (mode == IMATRIX) ? run_imatrix(model, seqs, base_path, prune_path, imat_min_hits) : run_kl_base(model, base_path, seqs); } } else { @@ -535,7 +611,7 @@ int main(int argc, char ** argv) { } if (ok) { rc = (mode == PPL) ? run_perplexity(model, seqs, spk_ptr, spk_pos) - : (mode == IMATRIX) ? run_imatrix(model, seqs, base_path, imat_min_hits) + : (mode == IMATRIX) ? run_imatrix(model, seqs, base_path, prune_path, imat_min_hits) : run_kl_base(model, base_path, seqs); } } diff --git a/src/prune-policy.h b/src/prune-policy.h new file mode 100644 index 0000000..0ac2285 --- /dev/null +++ b/src/prune-policy.h @@ -0,0 +1,108 @@ +// prune-policy.h — turn prune-stats into a per-layer keep-set. +// +// Shared by prune-cli (which bakes the keep-set into a smaller GGUF) and the runtime --prune-mask +// path (which applies it via zonos2_set_expert_mask) so a mask sweep and the final prune agree +// exactly. Header-only, depends only on prune-stats.h. +#pragma once + +#include "prune-stats.h" + +#include +#include +#include +#include + +namespace prune_policy { + +struct policy { + int keep_n = -1; // keep the N highest-MSAN experts per layer (uniform budget) + int drop_below_hits = -1; // (alt) keep experts routed at least this many times + double mass_eps = -1.0; // (alt) keep experts whose share of the layer's total MSAN >= eps + int drop_lowest = -1; // (alt) drop the N lowest-MSAN experts across ALL layers (global) +}; + +// Per-layer ascending kept expert ids. Experts are ranked by MSAN (then hit count, then id) and +// the layer never drops below its top-k. Only layers that actually lose an expert are returned — +// a layer absent from the result keeps all its experts. +// +// Modes (first set one wins): keep_n (uniform N/layer) > mass_eps (relative MSAN-mass threshold, +// per-layer non-uniform — prunes peaky deep layers hard while protecting flat early layers) > +// drop_below_hits. +inline std::map> select(const std::map & stats, + const policy & p) { + std::map> keep; + + // Global mode: drop the N lowest-MSAN experts across ALL layers at once (subject to each + // layer's top-k floor). Measures the marginal cost of removing the most-droppable experts + // anywhere, rather than a fixed budget per layer. + if (p.drop_lowest >= 0) { + struct cell { int L, e; float msan; int64_t cnt; }; + std::vector all; + for (const auto & kv : stats) + for (int e = 0; e < (int) kv.second.msan.size(); ++e) + all.push_back({ kv.first, e, kv.second.msan[e], kv.second.cnt[e] }); + std::sort(all.begin(), all.end(), [](const cell & a, const cell & b) { // lowest first + if (a.msan != b.msan) return a.msan < b.msan; + if (a.cnt != b.cnt) return a.cnt < b.cnt; + return a.L < b.L; + }); + std::map> dropped; // [L] which experts removed + int budget = p.drop_lowest; + for (const cell & c : all) { + if (budget <= 0) break; + const prune_stats::layer & pl = stats.at(c.L); + const int ne = (int) pl.msan.size(); + const int k = pl.top_k > 0 ? pl.top_k : 1; + auto & dl = dropped[c.L]; + if (dl.empty()) dl.assign(ne, 0); + int still = ne; for (char x : dl) if (x) --still; + if (still <= k) continue; // honor this layer's top-k floor + dl[c.e] = 1; --budget; + } + for (const auto & kv : dropped) { + std::vector ks; + for (int e = 0; e < (int) kv.second.size(); ++e) if (!kv.second[e]) ks.push_back(e); + if ((int) ks.size() != (int) stats.at(kv.first).msan.size()) keep[kv.first] = std::move(ks); + } + return keep; + } + + for (const auto & kv : stats) { + const prune_stats::layer & pl = kv.second; + const int ne = (int) pl.cnt.size(); + const int k = pl.top_k > 0 ? pl.top_k : 1; + if (ne == 0) continue; + + std::vector idx(ne); + std::iota(idx.begin(), idx.end(), 0); + std::sort(idx.begin(), idx.end(), [&](int a, int b) { // best (highest MSAN) first + if (pl.msan[a] != pl.msan[b]) return pl.msan[a] > pl.msan[b]; + if (pl.cnt[a] != pl.cnt[b]) return pl.cnt[a] > pl.cnt[b]; + return a < b; + }); + + int want; + if (p.keep_n >= 0) { + want = p.keep_n; + } else if (p.mass_eps >= 0.0) { // keep experts with MSAN share >= eps + double total = 0.0; + for (int e = 0; e < ne; ++e) total += pl.msan[e]; + want = 0; + if (total > 0.0) + for (int e = 0; e < ne; ++e) if (pl.msan[e] / total >= p.mass_eps) ++want; + } else { // drop-below-hits: keep cnt >= H + want = 0; + for (int e = 0; e < ne; ++e) if (pl.cnt[e] >= (int64_t) p.drop_below_hits) ++want; + } + if (want < k) want = k; // floor: top-k needs k experts + if (want > ne) want = ne; + if (want == ne) continue; // nothing to drop in this layer + + std::vector ks(idx.begin(), idx.begin() + want); + std::sort(ks.begin(), ks.end()); // ascending for determinism + keep[kv.first] = std::move(ks); + } + return keep; +} + +} // namespace prune_policy diff --git a/src/prune-stats.h b/src/prune-stats.h new file mode 100644 index 0000000..df83dd6 --- /dev/null +++ b/src/prune-stats.h @@ -0,0 +1,78 @@ +// prune-stats.h — per-(layer,expert) importance for MoE expert pruning. +// +// One record per MoE layer: the routed hit count and the mean-squared-activation-norm +// (MSAN) of every expert. MSAN is the (1,0,2) score from the unified one-shot expert- +// pruning formulation (arXiv:2606.15716) — mean over routed tokens of ‖f‖₂², where f is the +// expert's down-projection input silu(gate(x))·up(x). The literature finds activation-norm +// scores (MAN/MSAN) clearly beat routing frequency alone for deciding which experts to drop. +// +// Written by `zonos2-perplexity --prune-stats`, read by `prune-cli`. Header-only with no ggml +// dependency so prune-cli (ggml-only, like quantize-cli) can include it too. +#pragma once + +#include +#include +#include +#include +#include +#include + +namespace prune_stats { + +struct layer { + int32_t top_k = 0; // experts selected per token (from the model hparams) + std::vector cnt; // [n_expert] routed (token,expert) hit count + std::vector msan; // [n_expert] mean ‖down-input‖² over routed tokens +}; + +static const char MAGIC[8] = { 'Z', '2', 'P', 'R', 'U', 'N', 'E', '1' }; + +inline bool save(const std::string & path, const std::map & m) { + FILE * f = fopen(path.c_str(), "wb"); + if (!f) { fprintf(stderr, "prune-stats: cannot write %s\n", path.c_str()); return false; } + fwrite(MAGIC, 1, 8, f); + const uint32_t n = (uint32_t) m.size(); + fwrite(&n, sizeof n, 1, f); + for (const auto & kv : m) { + const int32_t L = kv.first; + const uint32_t ne = (uint32_t) kv.second.cnt.size(); + fwrite(&L, sizeof L, 1, f); + fwrite(&ne, sizeof ne, 1, f); + fwrite(&kv.second.top_k, sizeof(int32_t), 1, f); + fwrite(kv.second.cnt.data(), sizeof(int64_t), ne, f); + fwrite(kv.second.msan.data(), sizeof(float), ne, f); + } + fclose(f); + return true; +} + +inline bool load(const std::string & path, std::map & m) { + FILE * f = fopen(path.c_str(), "rb"); + if (!f) { fprintf(stderr, "prune-stats: cannot read %s\n", path.c_str()); return false; } + char magic[8]; + uint32_t n = 0; + if (fread(magic, 1, 8, f) != 8 || memcmp(magic, MAGIC, 8) != 0) { + fprintf(stderr, "prune-stats: %s bad magic\n", path.c_str()); fclose(f); return false; + } + if (fread(&n, sizeof n, 1, f) != 1) { fclose(f); return false; } + for (uint32_t i = 0; i < n; ++i) { + int32_t L = 0; + uint32_t ne = 0; + layer e; + if (fread(&L, sizeof L, 1, f) != 1) { fclose(f); return false; } + if (fread(&ne, sizeof ne, 1, f) != 1) { fclose(f); return false; } + if (fread(&e.top_k, sizeof(int32_t), 1, f) != 1) { fclose(f); return false; } + e.cnt.resize(ne); + e.msan.resize(ne); + if (fread(e.cnt.data(), sizeof(int64_t), ne, f) != ne || + fread(e.msan.data(), sizeof(float), ne, f) != ne) { + fprintf(stderr, "prune-stats: %s truncated layer %d\n", path.c_str(), L); + fclose(f); return false; + } + m.emplace(L, std::move(e)); + } + fclose(f); + return true; +} + +} // namespace prune_stats diff --git a/src/prune.cpp b/src/prune.cpp new file mode 100644 index 0000000..e0c9520 --- /dev/null +++ b/src/prune.cpp @@ -0,0 +1,172 @@ +// gguf -> gguf MoE expert pruner for zonos2 backbone files. +// +// Drops the least-important experts from each MoE layer and rewrites a smaller GGUF. Expert +// importance is the MSAN score (mean ‖down-input‖² over routed tokens) collected by +// `zonos2-perplexity --prune-stats`; per the one-shot MoE-pruning literature (arXiv:2606.15716) +// activation-norm scores rank experts far better than routing frequency alone. +// +// For each MoE layer we keep the top experts by MSAN and gather the corresponding slices of: +// - ffn_{gate,up,down}_exps.weight (3-D, expert = ne2) +// - router_mlp4.weight (2-D [router_dim, n_expert], expert = ne1) +// - router_bias (1-D [n_expert], expert = ne0) +// The router softmax then runs over the surviving logits, renormalizing the gate weights for +// free (see build_moe, which now reads the expert count per layer from the stack itself). +// Slicing is a pure per-expert memcpy gather — experts are whole contiguous blocks along their +// axis — so it works on an F16 *or* an already-quantized backbone, and composes with +// quantize-cli in either order. +// +// Usage: prune-cli --stats (--keep N | --drop-below-hits H) +// --keep N keep the N highest-MSAN experts in every MoE layer (uniform budget) +// --drop-below-hits H drop experts routed fewer than H times over the corpus (conservative) +// A layer never drops below its top-k (top-k selection needs at least k experts); when both the +// budget and the floor apply, the floor wins. expert_used_count_per_layer is lowered to match +// if a layer ends up with fewer experts than its old top-k. +#include "ggml.h" +#include "gguf.h" +#include "prune-stats.h" +#include "prune-policy.h" + +#include +#include +#include +#include +#include +#include +#include + +// blk.. prefix -> L, else -1. +static int layer_of(const std::string & n) { + if (n.rfind("blk.", 0) != 0) return -1; + const size_t p = 4, e = n.find('.', p); + if (e == std::string::npos) return -1; + return atoi(n.substr(p, e - p).c_str()); +} + +// Which tensor axis indexes the experts (and thus gets sliced), or -1 if this tensor is not +// per-expert and should be copied verbatim. +static int expert_axis(const std::string & n) { + if (n.find("_exps") != std::string::npos) return 2; // ffn_{gate,up,down}_exps + if (n.find(".router_mlp4.weight") != std::string::npos) return 1; // [router_dim, n_expert] + if (n.find(".router_bias") != std::string::npos) return 0; // [n_expert] + return -1; +} + +int main(int argc, char ** argv) { + std::string stats_path; + int keep_n = -1, drop_below_hits = -1; + double mass_eps = -1.0; + std::vector pos; + for (int i = 1; i < argc; i++) { + if (!strcmp(argv[i], "--stats") && i + 1 < argc) stats_path = argv[++i]; + else if (!strcmp(argv[i], "--keep") && i + 1 < argc) keep_n = atoi(argv[++i]); + else if (!strcmp(argv[i], "--mass-eps") && i + 1 < argc) mass_eps = atof(argv[++i]); + else if (!strcmp(argv[i], "--drop-below-hits") && i + 1 < argc) drop_below_hits = atoi(argv[++i]); + else pos.push_back(argv[i]); + } + if (pos.size() != 2 || stats_path.empty() || (keep_n < 0 && drop_below_hits < 0 && mass_eps < 0.0)) { + fprintf(stderr, + "usage: %s --stats (--keep N | --mass-eps E | --drop-below-hits H)\n" + " --stats f.bin per-(layer,expert) MSAN from `zonos2-perplexity --prune-stats`\n" + " --keep N keep the N highest-MSAN experts per MoE layer (uniform)\n" + " --mass-eps E keep experts with MSAN share >= E per layer (non-uniform: prunes\n" + " peaky deep layers hard, protects flat early layers)\n" + " --drop-below-hits H drop experts routed fewer than H times (conservative)\n" + " (a layer never drops below its top-k)\n", argv[0]); + return 1; + } + const char * in_path = pos[0], * out_path = pos[1]; + + std::map stats; + if (!prune_stats::load(stats_path, stats)) return 1; + + // Decide the keep-set (ascending original expert ids) for each MoE layer — shared with the + // runtime --prune-mask path so a mask sweep and this prune produce identical models. + const std::map> keep = + prune_policy::select(stats, { keep_n, drop_below_hits, mass_eps }); + if (keep.empty()) { + fprintf(stderr, "prune: budget drops no experts; nothing to do\n"); + return 1; + } + + ggml_context * ctx_in = nullptr; + gguf_init_params gp = { /*no_alloc=*/ false, /*ctx=*/ &ctx_in }; + gguf_context * gin = gguf_init_from_file(in_path, gp); + if (!gin) { fprintf(stderr, "prune: cannot open '%s'\n", in_path); return 1; } + const int64_t n_tensors = gguf_get_n_tensors(gin); + + // Pass 1: size the output pool (kept tensors unchanged, pruned tensors shrink to keep slabs). + size_t need = 0; + for (int64_t i = 0; i < n_tensors; i++) { + ggml_tensor * t = ggml_get_tensor(ctx_in, gguf_get_tensor_name(gin, i)); + const std::string n = ggml_get_name(t); + const int L = layer_of(n), ax = expert_axis(n); + auto it = (L >= 0) ? keep.find(L) : keep.end(); + if (ax >= 0 && it != keep.end()) need += it->second.size() * t->nb[ax]; + else need += ggml_nbytes(t); + } + need += (size_t) (n_tensors + 1) * ggml_tensor_overhead() + (1u << 20); + + ggml_context * ctx_out = ggml_init({ need, nullptr, /*no_alloc=*/ false }); + gguf_context * gout = gguf_init_empty(); + gguf_set_kv(gout, gin); // copy all metadata KVs + + // Pass 2: copy or gather tensor by tensor. + size_t in_bytes = 0, out_bytes = 0; + int max_experts = 0, n_pruned = 0; + for (int64_t i = 0; i < n_tensors; i++) { + ggml_tensor * t = ggml_get_tensor(ctx_in, gguf_get_tensor_name(gin, i)); + const std::string n = ggml_get_name(t); + const int L = layer_of(n), ax = expert_axis(n); + auto it = (L >= 0) ? keep.find(L) : keep.end(); + + ggml_tensor * d; + if (ax >= 0 && it != keep.end()) { // gather the kept experts + const std::vector & ks = it->second; + if ((int64_t) stats[L].cnt.size() != t->ne[ax]) { + fprintf(stderr, "prune: %s expert axis %lld != stats n_expert %zu\n", + n.c_str(), (long long) t->ne[ax], stats[L].cnt.size()); + return 1; + } + int64_t ne[GGML_MAX_DIMS]; + for (int k = 0; k < GGML_MAX_DIMS; ++k) ne[k] = t->ne[k]; + ne[ax] = (int64_t) ks.size(); + d = ggml_new_tensor(ctx_out, t->type, ggml_n_dims(t), ne); + ggml_set_name(d, n.c_str()); + const size_t stride = t->nb[ax]; // one expert = one contiguous slab + for (size_t e = 0; e < ks.size(); ++e) + memcpy((char *) d->data + e * stride, (char *) t->data + (size_t) ks[e] * stride, stride); + if (n.find("ffn_gate_exps") != std::string::npos) { ++n_pruned; + fprintf(stderr, " blk.%d %lld -> %zu experts\n", L, (long long) t->ne[ax], ks.size()); } + } else { // verbatim + d = ggml_new_tensor(ctx_out, t->type, ggml_n_dims(t), t->ne); + ggml_set_name(d, n.c_str()); + memcpy(d->data, t->data, ggml_nbytes(t)); + } + if (n.find("_exps") != std::string::npos) max_experts = std::max(max_experts, (int) d->ne[2]); + gguf_add_tensor(gout, d); + in_bytes += ggml_nbytes(t); + out_bytes += ggml_nbytes(d); + } + + // Update expert metadata: lower per-layer top-k if a layer now has fewer experts than its + // old top-k, and set expert_count to the new global maximum. + const int64_t euid = gguf_find_key(gin, "zonos2.expert_used_count_per_layer"); + if (euid >= 0 && gguf_get_arr_type(gin, euid) == GGUF_TYPE_INT32) { + const size_t nl = gguf_get_arr_n(gin, euid); + const int32_t * old = (const int32_t *) gguf_get_arr_data(gin, euid); + std::vector eu(old, old + nl); + for (const auto & kv : keep) + if (kv.first >= 0 && (size_t) kv.first < nl) + eu[kv.first] = std::min(eu[kv.first], (int32_t) kv.second.size()); + gguf_set_arr_data(gout, "zonos2.expert_used_count_per_layer", GGUF_TYPE_INT32, eu.data(), eu.size()); + } + if (max_experts > 0) gguf_set_val_u32(gout, "zonos2.expert_count", (uint32_t) max_experts); + + if (!gguf_write_to_file(gout, out_path, false)) { fprintf(stderr, "prune: write failed\n"); return 1; } + + fprintf(stderr, "prune: %s -> %s : pruned %d MoE layers, max %d experts/layer, %.2f GB -> %.2f GB\n", + in_path, out_path, n_pruned, max_experts, in_bytes / 1e9, out_bytes / 1e9); + + gguf_free(gout); ggml_free(ctx_out); gguf_free(gin); ggml_free(ctx_in); + return 0; +} diff --git a/src/server-embed.h b/src/server-embed.h new file mode 100644 index 0000000..b8ccbee --- /dev/null +++ b/src/server-embed.h @@ -0,0 +1,26 @@ +// In-process embedding hooks for the zonos2 HTTP server (used by zonos2-app). +// +// zonos2_server_main is the whole of zonos2-server's main(): it loads the models, +// spawns the worker/DAC threads, binds, and blocks serving requests until +// httplib::Server::stop() is called (or bind fails). A non-null `ctl` lets an +// embedder observe startup and shut the server down: +// +// zonos2_server_ctl ctl; +// std::thread th([&] { zonos2_server_main(argc, argv, &ctl); }); +// ...wait for ctl.port > 0 (listening) or ctl.failed... +// ctl.svr->stop(); // from any thread; zonos2_server_main then returns +// th.join(); +#pragma once + +#include + +namespace httplib { class Server; } + +struct zonos2_server_ctl { + std::atomic port{0}; // actual bound port once listening (--port 0 = ephemeral) + std::atomic failed{false};// set on any startup failure (bad args, model load, bind) + std::atomic svr{nullptr}; // set just before the accept loop starts, + // cleared before zonos2_server_main returns +}; + +int zonos2_server_main(int argc, char ** argv, zonos2_server_ctl * ctl); diff --git a/src/server.cpp b/src/server.cpp index 4b53cf4..280ae37 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -19,6 +19,8 @@ #include "dac.h" #include "spk-encoder.h" #include "npy.h" +#include "model-paths.h" +#include "server-embed.h" #include "httplib.h" #include "json.hpp" @@ -1489,9 +1491,10 @@ static std::string read_file(const std::string & path) { static void usage(const char * a0) { fprintf(stderr, - "usage: %s --dac [--spk ] [options]\n" + "usage: %s [--dac ] [--spk|--spk-encoder ] [options]\n" + " (dac.gguf / spk-encoder.gguf are auto-detected next to when the flags are omitted)\n" " --host H bind address (default 127.0.0.1)\n" - " --port P port (default 1919)\n" + " --port P port (default 1919; 0 picks a free ephemeral port)\n" " --gpu | --cpu backend (default cpu)\n" " --dac-cpu run the DAC decoder on CPU even with --gpu (isolates the\n" " backbone graph; makes streamed audio bit-exact; default on Metal)\n" @@ -1520,7 +1523,7 @@ static void usage(const char * a0) { " --ui PATH web UI html to serve at / (default web/tts_ui.html)\n", a0); } -int main(int argc, char ** argv) { +static int server_run(int argc, char ** argv, zonos2_server_ctl * ctl) { if (argc < 2) { usage(argv[0]); return 1; } ServerState s; @@ -1530,7 +1533,7 @@ int main(int argc, char ** argv) { for (int i = 2; i < argc; ++i) { std::string a = argv[i]; if (a == "--dac" && i + 1 < argc) dac_path = argv[++i]; - else if (a == "--spk" && i + 1 < argc) spk_path = argv[++i]; + else if ((a == "--spk" || a == "--spk-encoder") && i + 1 < argc) spk_path = argv[++i]; else if (a == "--host" && i + 1 < argc) host = argv[++i]; else if (a == "--port" && i + 1 < argc) port = atoi(argv[++i]); else if (a == "--gpu") s.use_gpu = true; @@ -1551,10 +1554,33 @@ int main(int argc, char ** argv) { else if (a == "--ui" && i + 1 < argc) s.ui_path = argv[++i]; else { usage(argv[0]); return 1; } } - if (dac_path.empty()) { fprintf(stderr, "error: --dac is required\n"); return 1; } + if (!mp_file_exists(model_path)) { + fprintf(stderr, "failed to load model %s\n", model_path.c_str()); + mp_print_download_hint("zonos2-q6_k.gguf (or another quant)", model_path); + return 1; + } + + // dac/spk-encoder conventionally sit next to the backbone; pick them up when unflagged. + if (dac_path.empty()) { + dac_path = mp_find_companion(model_path, "dac.gguf"); + if (!dac_path.empty()) fprintf(stderr, "zonos2-server: using dac.gguf found next to model: %s\n", dac_path.c_str()); + } + if (spk_path.empty()) { + spk_path = mp_find_companion(model_path, "spk-encoder.gguf"); + if (!spk_path.empty()) fprintf(stderr, "zonos2-server: using spk-encoder.gguf found next to model: %s\n", spk_path.c_str()); + } + if (dac_path.empty()) { + fprintf(stderr, "error: --dac is required (also auto-detected when dac.gguf sits next to )\n"); + mp_print_download_hint("dac.gguf", mp_parent_dir(model_path) + "/dac.gguf"); + return 1; + } s.prof = getenv("ZONOS2_PROFILE") != nullptr; - if (!zonos2_model_load(s.model, model_path.c_str(), s.use_gpu)) { fprintf(stderr, "failed to load model\n"); return 1; } + if (!zonos2_model_load(s.model, model_path.c_str(), s.use_gpu)) { + fprintf(stderr, "failed to load model %s\n", model_path.c_str()); + if (!mp_file_exists(model_path)) mp_print_download_hint("zonos2-q6_k.gguf (or another quant)", model_path); + return 1; + } { std::string err; s.have_emotion = zonos2_emotion_load(s.emotion, s.emotion_dir, err); @@ -1578,9 +1604,17 @@ int main(int argc, char ** argv) { fprintf(stderr, "zonos2-server: Metal GPU -> DAC defaults to CPU (avoids backbone contention; --dac-gpu to override)\n"); } } - if (!dac_load(s.dac, dac_path.c_str(), s.use_gpu && !dac_cpu)) { fprintf(stderr, "failed to load dac\n"); return 1; } + if (!dac_load(s.dac, dac_path.c_str(), s.use_gpu && !dac_cpu)) { + fprintf(stderr, "failed to load dac %s\n", dac_path.c_str()); + if (!mp_file_exists(dac_path)) mp_print_download_hint("dac.gguf", dac_path); + return 1; + } if (!spk_path.empty()) { - if (!spk_load(s.spk, spk_path.c_str())) { fprintf(stderr, "failed to load speaker encoder\n"); return 1; } + if (!spk_load(s.spk, spk_path.c_str())) { + fprintf(stderr, "failed to load speaker encoder %s\n", spk_path.c_str()); + if (!mp_file_exists(spk_path)) mp_print_download_hint("spk-encoder.gguf", spk_path); + return 1; + } s.have_spk = true; } @@ -1646,8 +1680,18 @@ int main(int argc, char ** argv) { fprintf(stderr, "zonos2-server: backbone=%s dac=%s spk=%s backend=%s batch=%d dac-threads=%d\n", model_path.c_str(), dac_path.c_str(), s.have_spk ? spk_path.c_str() : "(none)", s.use_gpu ? "GPU" : "CPU", s.batch_slots, s.dac_threads); - fprintf(stderr, "zonos2-server: listening on http://%s:%d (UI: %s)\n", host.c_str(), port, s.ui_path.c_str()); - const bool ok = svr.listen(host, port); + // Bind before announcing (and before publishing to ctl): --port 0 asks the OS + // for a free ephemeral port, which the embedding app reads back from ctl->port. + bool bound = true; + if (port == 0) { port = svr.bind_to_any_port(host); bound = port > 0; } + else bound = svr.bind_to_port(host, port); + + bool ok = false; + if (bound) { + fprintf(stderr, "zonos2-server: listening on http://%s:%d (UI: %s)\n", host.c_str(), port, s.ui_path.c_str()); + if (ctl) { ctl->svr = &svr; ctl->port.store(port); } + ok = svr.listen_after_bind(); + } s.stop.store(true); // stop the worker + DAC pool before returning s.sched_cv.notify_all(); @@ -1658,6 +1702,19 @@ int main(int argc, char ** argv) { if (lane.th.joinable()) lane.th.join(); dac_free(lane.dac); } - if (!ok) { fprintf(stderr, "error: failed to bind %s:%d\n", host.c_str(), port); return 1; } - return 0; + if (ctl) ctl->svr = nullptr; // svr is about to go out of scope + if (!bound) { fprintf(stderr, "error: failed to bind %s:%d\n", host.c_str(), port); return 1; } + // listen_after_bind() returns true on a graceful stop() and false only if the + // accept loop died on a real error. + return ok ? 0 : 1; } + +int zonos2_server_main(int argc, char ** argv, zonos2_server_ctl * ctl) { + const int rc = server_run(argc, argv, ctl); + if (rc != 0 && ctl) ctl->failed.store(true); + return rc; +} + +#ifndef ZONOS2_APP_EMBED +int main(int argc, char ** argv) { return zonos2_server_main(argc, argv, nullptr); } +#endif diff --git a/src/spk-encoder.cpp b/src/spk-encoder.cpp index 4f296bc..33a5ae4 100644 --- a/src/spk-encoder.cpp +++ b/src/spk-encoder.cpp @@ -4,6 +4,7 @@ // mono waveform (STFT via DFT matmul + slaney mel + log). CPU backend. #include "compat.h" #include "spk-encoder.h" +#include "exe-path.h" #include "ggml.h" #include "ggml-alloc.h" @@ -13,6 +14,7 @@ #include #include #include +#include #include #include #include @@ -295,9 +297,28 @@ static std::string shell_quote_path(const char * path) { #endif } +// Locate the ffmpeg used to decode reference audio for cloning. The launcher and the +// desktop app download an ffmpeg on demand and point us at it via ZONOS2_FFMPEG; we +// also look next to the executable (dist layout / macOS bundle Resources) before +// falling back to a bare "ffmpeg" on PATH. +static std::string resolve_ffmpeg() { + if (const char * e = getenv("ZONOS2_FFMPEG"); e && *e) return e; + namespace fs = std::filesystem; +#ifdef _WIN32 + const char * name = "ffmpeg.exe"; +#else + const char * name = "ffmpeg"; +#endif + std::error_code ec; + const fs::path dir = zonos2_exe_dir(); + for (const fs::path c : { dir / name, dir / "bin" / name, dir / ".." / "Resources" / name }) + if (fs::exists(c, ec)) return c.lexically_normal().string(); + return name; // rely on PATH +} + std::vector spk_decode_audio_file(const spk_model & m, const char * path) { // decode any audio via ffmpeg -> 24 kHz mono f32 - std::string cmd = "ffmpeg -v error -i " + shell_quote_path(path) + + std::string cmd = shell_quote_path(resolve_ffmpeg().c_str()) + " -v error -i " + shell_quote_path(path) + " -ac 1 -ar " + std::to_string(m.sr) + " -f f32le -"; #ifdef _WIN32 // binary mode: text mode eats 0x1A as EOF and mangles CRLF in the raw f32 diff --git a/src/zonos2-graph.cpp b/src/zonos2-graph.cpp index 3f93ee6..18667dc 100644 --- a/src/zonos2-graph.cpp +++ b/src/zonos2-graph.cpp @@ -191,7 +191,10 @@ static ggml_tensor * build_dense_ffn(gctx & g, ggml_tensor * cur, const zonos2_l static ggml_tensor * build_moe(gctx & g, ggml_tensor * cur, const zonos2_layer & ly, int L) { const zonos2_hparams & hp = g.m->hp; ggml_context * ctx = g.ctx; - const int ne = (int) hp.n_expert, k = ly.top_k, n = g.n, n_embd = (int) hp.n_embd; + // Per-layer expert count (not hp.n_expert): a pruned GGUF can keep a different number of + // experts per layer, so derive it from the actual stack. The router_mlp4/router_bias the + // softmax runs over are sliced to match, so softmax renormalizes over the survivors. + const int ne = (int) ly.ffn_up_exps->ne[2], k = ly.top_k, n = g.n, n_embd = (int) hp.n_embd; ggml_tensor * rh = ggml_add(ctx, ggml_mul_mat(ctx, ly.router_down, cur), ly.router_down_b); // [rd, n] if (ly.router_eda_scale && g.router_states) { @@ -204,9 +207,15 @@ static ggml_tensor * build_moe(gctx & g, ggml_tensor * cur, const zonos2_layer & ggml_tensor * h = ggml_gelu_erf(ctx, ggml_add(ctx, ggml_mul_mat(ctx, ly.router_mlp0, rn), ly.router_mlp0_b)); h = ggml_gelu_erf(ctx, ggml_add(ctx, ggml_mul_mat(ctx, ly.router_mlp2, h), ly.router_mlp2_b)); ggml_tensor * rlogits = ggml_mul_mat(ctx, ly.router_mlp4, h); // [ne, n] + // Runtime expert mask (--prune-mask): -inf on dropped experts before softmax, so the + // survivors' probabilities renormalize exactly as in a prune-cli'd model (broadcasts over n). + if (ly.router_mask) rlogits = ggml_add(ctx, rlogits, ly.router_mask); ggml_tensor * probs = ggml_soft_max(ctx, rlogits); // [ne, n] ggml_tensor * scores = ggml_add(ctx, probs, ly.router_bias); // legacy: probs + bias + // Re-apply the mask to the selection scores too: a dropped expert's prob is already 0, but + // probs+bias could still rank it into the top-k, so force it to -inf here as well. + if (ly.router_mask) scores = ggml_add(ctx, scores, ly.router_mask); ggml_tensor * sel = ggml_top_k(ctx, scores, k); // [k, n] i32 ggml_tensor * weights = ggml_get_rows(ctx, ggml_reshape_3d(ctx, probs, 1, ne, n), sel); // [1, k, n] @@ -321,6 +330,16 @@ ggml_tensor * build_graph(gctx & g, int n_layer_limit) { const std::string s = std::to_string(L); res = res ? ggml_add(ctx, x, res) : x; + + // depth-prune (--skip-layers): bypass this whole block. `res` already folds every prior + // block's contribution, so passing it through unchanged is an exact identity; x must carry + // no pending FFN (it was just folded above), so zero it — the next iteration's fold then + // adds nothing. Attention + FFN for this block are never built (real compute/KV savings). + if (L < (int) m.layer_skip.size() && m.layer_skip[L]) { + x = ggml_scale(ctx, res, 0.0f); + continue; + } + ggml_tensor * cur = g.cap("attn_in_" + s, rms_w(ctx, res, ly.attn_norm, hp.rms_eps)); ggml_tensor * attn = g.cap("attn_out_" + s, build_attention(g, cur, ly, L)); res = g.cap("layer_res_" + s, ggml_add(ctx, attn, res)); diff --git a/src/zonos2.cpp b/src/zonos2.cpp index df39f24..c64613e 100644 --- a/src/zonos2.cpp +++ b/src/zonos2.cpp @@ -4,6 +4,7 @@ #include "gguf.h" #include +#include #include #include @@ -185,12 +186,58 @@ bool zonos2_model_load(zonos2_model & m, const char * path, bool use_gpu) { } void zonos2_model_free(zonos2_model & m) { + if (m.buf_mask) ggml_backend_buffer_free(m.buf_mask); + if (m.ctx_mask) ggml_free(m.ctx_mask); if (m.buf_w) ggml_backend_buffer_free(m.buf_w); if (m.ctx_w) ggml_free(m.ctx_w); if (m.backend) ggml_backend_free(m.backend); m = zonos2_model(); } +// Runtime expert mask — the dynamic equivalent of prune-cli. For each MoE layer in `keep` we +// build an additive [n_expert] vector (0 for kept experts, -inf for dropped) and hang it on the +// layer; build_moe adds it to the router logits before softmax (so the surviving experts' +// probabilities renormalize) and to the top-k selection scores (so dropped experts are never +// routed). The keep-set is identical to what prune-cli would bake, so a mask sweep predicts the +// pruned model's quality without writing any GGUF. +bool zonos2_set_expert_mask(zonos2_model & m, const std::map> & keep) { + // Clear any previous mask first. + if (m.buf_mask) { ggml_backend_buffer_free(m.buf_mask); m.buf_mask = nullptr; } + if (m.ctx_mask) { ggml_free(m.ctx_mask); m.ctx_mask = nullptr; } + for (auto & ly : m.layers) ly.router_mask = nullptr; + + // Collect the layers we will actually mask (MoE, in range, present in keep). + std::vector mlayers; + for (const auto & kv : keep) { + const int L = kv.first; + if (L >= 0 && L < (int) m.layers.size() && m.layers[L].is_moe) mlayers.push_back(L); + } + if (mlayers.empty()) return true; + + ggml_init_params ip = { (size_t) (mlayers.size() + 1) * ggml_tensor_overhead(), nullptr, /*no_alloc=*/ true }; + m.ctx_mask = ggml_init(ip); + if (!m.ctx_mask) { fprintf(stderr, "zonos2: expert-mask ctx alloc failed\n"); return false; } + for (int L : mlayers) { + const int ne = (int) m.layers[L].ffn_up_exps->ne[2]; + ggml_tensor * t = ggml_new_tensor_1d(m.ctx_mask, GGML_TYPE_F32, ne); + ggml_set_name(t, ("router_mask." + std::to_string(L)).c_str()); + m.layers[L].router_mask = t; + } + m.buf_mask = ggml_backend_alloc_ctx_tensors(m.ctx_mask, m.backend); + if (!m.buf_mask) { fprintf(stderr, "zonos2: expert-mask buffer alloc failed\n"); return false; } + + int total_dropped = 0; + for (int L : mlayers) { + const int ne = (int) m.layers[L].ffn_up_exps->ne[2]; + std::vector h(ne, -INFINITY); + for (int e : keep.at(L)) if (e >= 0 && e < ne) h[e] = 0.0f; + for (int e = 0; e < ne; ++e) if (h[e] != 0.0f) ++total_dropped; + ggml_backend_tensor_set(m.layers[L].router_mask, h.data(), 0, h.size() * sizeof(float)); + } + fprintf(stderr, "zonos2: expert mask on %zu MoE layers, %d experts dropped\n", mlayers.size(), total_dropped); + return true; +} + int zonos2_router_dim(const zonos2_model & m) { for (const auto & ly : m.layers) { if (ly.is_moe && ly.router_down) return (int) ly.router_down->ne[1]; diff --git a/src/zonos2.h b/src/zonos2.h index f9ee4a6..4fd1af8 100644 --- a/src/zonos2.h +++ b/src/zonos2.h @@ -80,6 +80,7 @@ struct zonos2_layer { struct ggml_tensor * router_norm = nullptr; // [router_dim] struct ggml_tensor * router_bias = nullptr; // [n_expert] struct ggml_tensor * router_eda_scale = nullptr; // [router_dim], null on first MoE layer + struct ggml_tensor * router_mask = nullptr; // [n_expert] additive: 0 keep / -inf dropped (runtime --prune-mask) bool is_moe = false; int top_k = 0; @@ -106,6 +107,17 @@ struct zonos2_model { ggml_backend_buffer_t buf_w = nullptr; std::map tensors; bool is_gpu = false; + + // optional runtime expert mask (set by zonos2_set_expert_mask; see ly.router_mask) + struct ggml_context * ctx_mask = nullptr; + ggml_backend_buffer_t buf_mask = nullptr; + + // optional runtime depth-prune: transformer blocks whose index has a nonzero entry here are + // bypassed at graph-build time — the block's attention + FFN are not built and the residual + // stream passes through unchanged (identity). Empty (or all-zero) => every block runs. Set + // directly on the model before building a graph; every prefill/decode path honors it. Used to + // sweep the depth-pruning quality knee (leave-one-out / contiguous-span KLD) before baking. + std::vector layer_skip; // size 0 or n_layer; layer_skip[L] != 0 => skip block L }; // Load a zonos2 GGUF onto the CPU or first GPU backend. Returns false on error. @@ -115,6 +127,14 @@ void zonos2_model_free(zonos2_model & model); // router hidden dimension, derived from the router_down weight (ne1). int zonos2_router_dim(const zonos2_model & model); +// Install a runtime expert mask: for each layer id in `keep`, only the listed expert ids stay +// routable — the others get -inf router logits, so the softmax renormalizes over the survivors +// and top-k never selects them. This is the runtime equivalent of prune-cli (same keep-set), for +// sweeping the quality knee without rewriting GGUFs. Layers absent from `keep` are unmasked; +// passing {} clears any previous mask. The mask lives on the model, so every graph path (validate, +// logits, generate, batch, moe_capture) applies it automatically. Returns false on alloc failure. +bool zonos2_set_expert_mask(zonos2_model & model, const std::map> & keep); + // Run a single prefill forward over `ids` (row-major [n_tokens, n_codebooks+1], values // as floats), capturing named intermediate tensors as /.npy for numeric // validation against the PyTorch golden dump. `n_layer_limit` < 0 builds all layers. diff --git a/tests/launcher-smoke.sh b/tests/launcher-smoke.sh new file mode 100755 index 0000000..b1c29ec --- /dev/null +++ b/tests/launcher-smoke.sh @@ -0,0 +1,147 @@ +#!/usr/bin/env bash +# Exercise scripts/start-zonos2.sh against the tiny committed fixtures: the +# download-skip path (models already present), the download path (fetch from a +# local http.server), the optional-spk-encoder warning, and trap teardown. +# +# Uses SIGTERM (not SIGINT) to test cleanup: background non-interactive shells +# have SIGINT ignored on entry, so its trap can never fire here — interactively +# (the real double-click case) Ctrl-C works through the same trap. +# +# Usage: bash tests/launcher-smoke.sh [server-bin] +# binary defaults to auto-discovery under build*/. Run from anywhere. +set -euo pipefail + +ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" +cd "$ROOT" + +LAUNCHER="scripts/start-zonos2.sh" +MODEL="tests/fixtures/dummy-zonos2.gguf" +DAC="tests/fixtures/dummy-dac.gguf" + +find_bin() { + if [ -n "${1:-}" ]; then echo "$1"; return; fi + find build* \( -name zonos2-server -o -name zonos2-server.exe \) -type f 2>/dev/null | head -1 +} +SRV="$(find_bin "${1:-}")" +[ -x "$SRV" ] || { echo "FAIL: zonos2-server not found (pass it as \$1)"; exit 1; } +[ -f "$MODEL" ] || { echo "FAIL: missing $MODEL"; exit 1; } +[ -f "$DAC" ] || { echo "FAIL: missing $DAC"; exit 1; } +command -v curl >/dev/null 2>&1 || { echo "SKIP: curl not available"; exit 0; } +command -v python3 >/dev/null 2>&1 || { echo "SKIP: python3 not available"; exit 0; } + +WORK="$(mktemp -d)" +LNCH_PID="" +HTTP_PID="" +cleanup() { + local rc=$? + [ -n "$LNCH_PID" ] && { kill "$LNCH_PID" 2>/dev/null || true; wait "$LNCH_PID" 2>/dev/null || true; } + [ -n "$HTTP_PID" ] && { kill "$HTTP_PID" 2>/dev/null || true; wait "$HTTP_PID" 2>/dev/null || true; } + rm -rf "$WORK" + exit "$rc" +} +trap cleanup EXIT +pass() { echo " ok: $1"; } +fail() { echo "FAIL: $1"; [ -f "$WORK/launch.log" ] && { echo "--- launcher log ---"; cat "$WORK/launch.log"; }; exit 1; } +is_riff() { [ "$(head -c4 "$1" 2>/dev/null)" = "RIFF" ]; } + +PORT="${ZONOS2_TEST_PORT:-19188}" +BASE="http://127.0.0.1:$PORT" + +# run_launcher : start the launcher against $WORK/models, wait for /health. +run_launcher() { + ZONOS2_MODEL_DIR="$WORK/models" ZONOS2_SERVER_BIN="$SRV" \ + ZONOS2_PORT="$PORT" ZONOS2_BASE_URL="$1" \ + ZONOS2_ASSUME_YES=1 ZONOS2_NO_BROWSER=1 \ + bash "$LAUNCHER" -- --max 16 >"$WORK/launch.log" 2>&1 & + LNCH_PID=$! + local up=0 + for _ in $(seq 1 150); do + if curl -fsS "$BASE/health" >/dev/null 2>&1; then up=1; break; fi + kill -0 "$LNCH_PID" 2>/dev/null || fail "launcher exited before the server came up" + sleep 0.2 + done + [ "$up" = 1 ] || fail "server did not answer /health in time" +} + +# stop_launcher: TERM the launcher, assert its trap takes the server down. +stop_launcher() { + kill -TERM "$LNCH_PID" 2>/dev/null || true + wait "$LNCH_PID" 2>/dev/null || true + LNCH_PID="" + local down=0 + for _ in $(seq 1 25); do + if ! curl -fsS "$BASE/health" >/dev/null 2>&1; then down=1; break; fi + sleep 0.2 + done + [ "$down" = 1 ] || fail "server still answering after the launcher was stopped" +} + +echo "== launcher: download-skip path ==" +mkdir -p "$WORK/models" +cp "$MODEL" "$WORK/models/zonos2-q6_k.gguf" +cp "$DAC" "$WORK/models/dac.gguf" +# Poisoned base URL: backbone+dac are present so no fatal download may happen +# (the missing spk-encoder.gguf fails fast and must only warn). +run_launcher "http://127.0.0.1:9" +# the launcher's own readiness poll may print a beat after ours succeeds +ready=0 +for _ in $(seq 1 25); do + if grep -q "zonos2: ready" "$WORK/launch.log"; then ready=1; break; fi + sleep 0.2 +done +[ "$ready" = 1 ] || fail "no ready line in launcher output" +pass "models present -> server up without downloads" +grep -q "warning: failed to download spk-encoder.gguf" "$WORK/launch.log" \ + || fail "missing spk-encoder.gguf did not produce the non-fatal warning" +pass "spk-encoder download failure is non-fatal" +curl -fsS "$BASE/tts/generate" \ + -d '{"text":"launcher smoke","stream":false,"format":"wav","seed":1,"max_tokens":16}' \ + -o "$WORK/smoke.wav" || fail "/tts/generate request failed" +is_riff "$WORK/smoke.wav" || fail "/tts/generate did not return a RIFF WAV" +pass "/tts/generate -> WAV" +stop_launcher +pass "TERM tears the server down" + +echo "== launcher: ffmpeg auto-download ==" +# Serve a fake single-binary ffmpeg and force the download (ZONOS2_SKIP_PATH_FFMPEG bypasses +# the system-ffmpeg check that CI/dev boxes would otherwise satisfy). Models already present, +# so only ffmpeg is fetched. +FF_PORT="${ZONOS2_TEST_FF_PORT:-18124}" +mkdir -p "$WORK/ffserve" +printf '#!/bin/sh\necho fake-ffmpeg "$@"\n' > "$WORK/ffserve/ffmpeg" +python3 -m http.server "$FF_PORT" --bind 127.0.0.1 --directory "$WORK/ffserve" >/dev/null 2>&1 & +HTTP_PID=$! +for _ in $(seq 1 50); do curl -fsS "http://127.0.0.1:$FF_PORT/" >/dev/null 2>&1 && break; sleep 0.2; done +rm -rf "$WORK/models/bin" +export ZONOS2_SKIP_PATH_FFMPEG=1 ZONOS2_FFMPEG_URL="http://127.0.0.1:$FF_PORT/ffmpeg" +run_launcher "http://127.0.0.1:9" +[ -x "$WORK/models/bin/ffmpeg" ] || fail "ffmpeg was not downloaded to models/bin" +pass "ffmpeg auto-downloaded to models/bin" +grep -q "downloading ffmpeg" "$WORK/launch.log" || fail "launcher did not announce the ffmpeg download" +pass "ffmpeg download announced" +stop_launcher +unset ZONOS2_SKIP_PATH_FFMPEG ZONOS2_FFMPEG_URL +kill "$HTTP_PID" 2>/dev/null || true; wait "$HTTP_PID" 2>/dev/null || true; HTTP_PID="" + +echo "== launcher: download path ==" +HTTP_PORT="${ZONOS2_TEST_HTTP_PORT:-18123}" +mkdir -p "$WORK/serve" +cp "$MODEL" "$WORK/serve/zonos2-q6_k.gguf" +cp "$DAC" "$WORK/serve/dac.gguf" # deliberately no spk-encoder.gguf (404 -> warn) +python3 -m http.server "$HTTP_PORT" --bind 127.0.0.1 --directory "$WORK/serve" >/dev/null 2>&1 & +HTTP_PID=$! +srv_up=0 +for _ in $(seq 1 50); do + if curl -fsS "http://127.0.0.1:$HTTP_PORT/" >/dev/null 2>&1; then srv_up=1; break; fi + sleep 0.2 +done +[ "$srv_up" = 1 ] || fail "python http.server did not come up" + +rm "$WORK/models/dac.gguf" +run_launcher "http://127.0.0.1:$HTTP_PORT" +cmp -s "$WORK/models/dac.gguf" "$DAC" || fail "downloaded dac.gguf is not byte-identical to the fixture" +pass "missing dac.gguf downloaded and byte-identical" +stop_launcher +pass "second run torn down" + +echo "All launcher smoke tests passed." diff --git a/tests/readme-commands.sh b/tests/readme-commands.sh index 50520b7..af06792 100755 --- a/tests/readme-commands.sh +++ b/tests/readme-commands.sh @@ -67,6 +67,17 @@ pass "--tts -> codes .npy" is_riff "$WORK/t.wav" || fail "--tts --dac did not write a RIFF WAV" pass "--tts --dac -> WAV" +# same, with --dac dropped: dac.gguf sitting next to the backbone is auto-detected +# (README CLI section's models/ example) +mkdir -p "$WORK/models" +cp "$MODEL" "$WORK/models/zonos2-q6_k.gguf" +cp "$DAC" "$WORK/models/dac.gguf" +out=$("$CLI" "$WORK/models/zonos2-q6_k.gguf" --tts "ci tts autodiscover" "$WORK/auto.wav" --max 8 --greedy 2>&1) \ + || { echo "$out"; fail "--tts with sibling dac.gguf exited nonzero"; } +echo "$out" | grep -q "using dac.gguf found next to model" || { echo "$out"; fail "no dac auto-discovery line"; } +is_riff "$WORK/auto.wav" || fail "--tts with sibling dac.gguf did not write a RIFF WAV" +pass "--tts (sibling dac.gguf auto-detected) -> WAV" + # standalone codes -> wav (dac-cli), if present DACCLI="$(find_bin dac-cli "")" if [ -x "$DACCLI" ]; then