diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 000000000..5ff4c3958 --- /dev/null +++ b/.github/workflows/build.yml @@ -0,0 +1,44 @@ +name: Build + +on: + pull_request: + paths: + - cactus/** + - cactus-engine/** + - cactus-graph/** + - cactus-kernels/** + - apple/** + - android/** + - .github/workflows/build.yml + push: + branches: [main] + paths: + - cactus/** + - cactus-engine/** + - cactus-graph/** + - cactus-kernels/** + - apple/** + - android/** + - .github/workflows/build.yml + workflow_dispatch: + +permissions: + contents: read + +jobs: + apple: + runs-on: macos-15 + timeout-minutes: 15 + steps: + - uses: actions/checkout@v4 + - run: bash cactus/build.sh + - run: BUILD_STATIC=true BUILD_XCFRAMEWORK=false bash apple/build.sh + - run: bash android/build.sh + + linux: + runs-on: ubuntu-24.04-arm + timeout-minutes: 15 + steps: + - uses: actions/checkout@v4 + - run: sudo apt-get update && sudo apt-get install -y cmake build-essential libcurl4-openssl-dev + - run: bash cactus/build.sh diff --git a/.github/workflows/cpp.yml b/.github/workflows/cpp.yml deleted file mode 100644 index 3ff7868fe..000000000 --- a/.github/workflows/cpp.yml +++ /dev/null @@ -1,52 +0,0 @@ -name: C++ CI - -on: - pull_request: - paths: - - cactus/** - - cactus-engine/** - - cactus-graph/** - - cactus-kernels/** - - .github/workflows/cpp.yml - push: - branches: [main] - paths: - - cactus/** - - cactus-engine/** - - cactus-graph/** - - cactus-kernels/** - - .github/workflows/cpp.yml - workflow_dispatch: - -permissions: - contents: read - -jobs: - test: - timeout-minutes: 45 - strategy: - fail-fast: false - matrix: - include: - - os: macos-15 - deps: brew install ccache ninja - - os: ubuntu-24.04-arm - deps: sudo apt-get update && sudo apt-get install -y cmake build-essential libcurl4-openssl-dev ccache ninja-build - runs-on: ${{ matrix.os }} - steps: - - uses: actions/checkout@v4 - - run: ${{ matrix.deps }} - - uses: actions/cache@v4 - with: - path: ~/.ccache - key: ccache-${{ matrix.os }}-${{ github.ref_name }}-${{ hashFiles('cactus/CMakeLists.txt', 'cactus-engine/tests/CMakeLists.txt', '.github/workflows/cpp.yml') }} - restore-keys: | - ccache-${{ matrix.os }}-${{ github.ref_name }}- - ccache-${{ matrix.os }}- - - run: ccache --zero-stats || true - - run: cmake -G Ninja -S cactus -B cactus/build -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER_LAUNCHER=ccache - - run: cmake --build cactus/build --target cactus --parallel 4 - - run: cmake -G Ninja -S cactus-engine/tests -B cactus-engine/tests/build-ci -DCMAKE_BUILD_TYPE=Release -DCMAKE_CXX_COMPILER_LAUNCHER=ccache - - run: cmake --build cactus-engine/tests/build-ci --target test_stt --parallel 4 - - run: ./cactus-engine/tests/build-ci/test_stt - - run: ccache --show-stats || true diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml index 8150c1a4a..67fb7296f 100644 --- a/.github/workflows/docs.yml +++ b/.github/workflows/docs.yml @@ -16,6 +16,11 @@ on: - bindings/flutter/README.md - bindings/rust/README.md - README.md + workflow_call: + inputs: + version: + type: string + required: false workflow_dispatch: jobs: @@ -40,9 +45,11 @@ jobs: run: | git config user.name "github-actions[bot]" git config user.email "github-actions[bot]@users.noreply.github.com" - TAG=$(cat CACTUS_VERSION | tr -d '[:space:]') + TAG="${{ inputs.version || '' }}" + [ -z "$TAG" ] && TAG=$(cat CACTUS_VERSION | tr -d '[:space:]') bash .github/assemble-docs.sh "$TAG" mike deploy "$TAG" latest --update-aliases --push + mike set-default latest --push git checkout gh-pages echo "docs.cactuscompute.com" > CNAME diff --git a/.github/workflows/inference.yml b/.github/workflows/inference.yml new file mode 100644 index 000000000..47c49cb40 --- /dev/null +++ b/.github/workflows/inference.yml @@ -0,0 +1,115 @@ +name: Inference Tests + +on: + pull_request_review: + types: [submitted] + workflow_dispatch: + +permissions: + contents: read + +jobs: + gate: + runs-on: ubuntu-latest + outputs: + approved: ${{ steps.check.outputs.approved }} + steps: + - id: check + run: | + if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then + echo "approved=true" >> $GITHUB_OUTPUT + elif [ "${{ github.event.review.state }}" = "approved" ]; then + echo "approved=true" >> $GITHUB_OUTPUT + else + echo "approved=false" >> $GITHUB_OUTPUT + fi + + build: + needs: gate + if: needs.gate.outputs.approved == 'true' + runs-on: [self-hosted, linux, arm64] + steps: + - uses: actions/checkout@v4 + with: + clean: false + + - name: Setup Python environment + run: | + if [ ! -f venv/bin/activate ]; then + source ./setup + fi + + - name: Convert models + env: + HF_TOKEN: ${{ secrets.HF_TOKEN }} + run: | + source venv/bin/activate + convert() { + local id=$1 dir=$2 bits=$3 + [ -f "weights/$dir/config.txt" ] && return + python3 -m cactus convert "$id" --bits "$bits" ${HF_TOKEN:+--token "$HF_TOKEN"} || true + } + convert "Qwen/Qwen3-0.6B" "qwen3-0.6b" "4" + convert "google/gemma-3-270m-it" "gemma-3-270m-it" "4" + convert "openai/whisper-small" "whisper-small" "4" + convert "nvidia/parakeet-tdt-0.6b-v3" "parakeet-tdt-0.6b-v3" "4" + + - name: Build + run: | + bash cactus/build.sh + cd cactus-engine/tests + rm -rf build && mkdir -p build && cd build + cmake .. -DCMAKE_BUILD_TYPE=Release + make -j$(nproc) + + unit-tests: + needs: build + runs-on: [self-hosted, linux, arm64] + env: + CACTUS_INDEX_PATH: /tmp/cactus_test_index + CACTUS_NO_CLOUD_TELE: "1" + steps: + - uses: actions/checkout@v4 + with: + clean: false + - name: Run unit tests + run: | + mkdir -p "$CACTUS_INDEX_PATH" + cd cactus-engine/tests/build + ./test_index + ./test_model_loading + ./test_telemetry + + - name: Run kernel tests + run: bash cactus-kernels/test.sh + + - name: Run graph tests + run: bash cactus-graph/test.sh + + inference-tests: + needs: build + runs-on: [self-hosted, linux, arm64] + env: + CACTUS_TEST_ASSETS: ${{ github.workspace }}/cactus-engine/tests/assets + CACTUS_NO_CLOUD_TELE: "1" + strategy: + fail-fast: false + matrix: + include: + - test: test_llm + model_dir: qwen3-0.6b + - test: test_llm + model_dir: gemma-3-270m-it + - test: test_stt + model_dir: whisper-small + - test: test_stt + model_dir: parakeet-tdt-0.6b-v3 + steps: + - uses: actions/checkout@v4 + with: + clean: false + - name: Run ${{ matrix.test }} (${{ matrix.model_dir }}) + env: + CACTUS_TEST_MODEL: ${{ github.workspace }}/weights/${{ matrix.model_dir }} + CACTUS_TEST_TRANSCRIBE_MODEL: ${{ github.workspace }}/weights/${{ matrix.model_dir }} + run: ./cactus-engine/tests/build/${{ matrix.test }} diff --git a/.github/workflows/pypi.yml b/.github/workflows/pypi.yml new file mode 100644 index 000000000..dd36b34d9 --- /dev/null +++ b/.github/workflows/pypi.yml @@ -0,0 +1,83 @@ +name: PyPI Publish + +on: + workflow_call: + +jobs: + wheels: + strategy: + matrix: + include: + - os: macos-14 + lib: libcactus.dylib + plat: macosx_14_0_arm64 + - os: ubuntu-24.04-arm + lib: libcactus.so + plat: manylinux_2_17_aarch64 + runs-on: ${{ matrix.os }} + steps: + - uses: actions/checkout@v4 + + - name: Install build tools + run: | + if [[ "$RUNNER_OS" == "Linux" ]]; then + sudo apt-get update && sudo apt-get install -y cmake build-essential libcurl4-openssl-dev + fi + + - name: Build libcactus + run: bash cactus/build.sh + + - name: Bundle library into package + run: | + mkdir -p python/cactus/bindings/lib + cp cactus/build/${{ matrix.lib }} python/cactus/bindings/lib/ + + - uses: actions/setup-python@v5 + with: + python-version: "3.12" + + - name: Build wheel + run: | + pip install build wheel + cd python + python -m build --wheel -C="--build-option=--plat-name=${{ matrix.plat }}" + + - uses: actions/upload-artifact@v4 + with: + name: wheel-${{ matrix.os }} + path: python/dist/*.whl + + sdist: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + + - uses: actions/setup-python@v5 + with: + python-version: "3.12" + + - name: Build sdist + run: | + pip install build + cd python + python -m build --sdist + + - uses: actions/upload-artifact@v4 + with: + name: sdist + path: python/dist/*.tar.gz + + publish: + needs: [wheels, sdist] + runs-on: ubuntu-latest + permissions: + id-token: write + steps: + - uses: actions/download-artifact@v4 + with: + path: dist + merge-multiple: true + + - uses: pypa/gh-action-pypi-publish@release/v1 + with: + packages-dir: dist diff --git a/.github/workflows/python.yml b/.github/workflows/python.yml new file mode 100644 index 000000000..253426e6b --- /dev/null +++ b/.github/workflows/python.yml @@ -0,0 +1,36 @@ +name: Python Tests + +on: + pull_request: + paths: + - "python/**" + - ".github/workflows/python.yml" + push: + branches: [main] + paths: + - "python/**" + - ".github/workflows/python.yml" + +jobs: + test: + runs-on: ubuntu-24.04-arm + steps: + - uses: actions/checkout@v4 + + - name: Install build tools + run: sudo apt-get update && sudo apt-get install -y cmake build-essential libcurl4-openssl-dev + + - name: Build libcactus + run: bash cactus/build.sh + + - uses: actions/setup-python@v5 + with: + python-version: "3.12" + + - name: Install dependencies + run: pip install -e "python/[dev]" + + - name: Run tests + run: | + cd python + pytest tests/ -v --ignore=tests/test_model.py diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index cdb64fe18..36c100cce 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,95 +1,75 @@ -name: Release & Update Homebrew +name: Release on: - schedule: - - cron: "0 21 * * 1" # Every Monday at 9pm UTC - workflow_dispatch: + release: + types: [published] jobs: - release: + pypi: + uses: ./.github/workflows/pypi.yml + + homebrew: + needs: pypi runs-on: ubuntu-latest - permissions: - contents: write steps: - - uses: actions/checkout@v4 - with: - fetch-depth: 0 - fetch-tags: true - - - name: Determine version - id: version - run: | - TAG=$(cat CACTUS_VERSION | tr -d '[:space:]') - if git rev-parse "${TAG}" >/dev/null 2>&1; then - echo "Tag ${TAG} already exists. Skipping." - echo "skip=true" >> $GITHUB_OUTPUT - else - echo "tag=${TAG}" >> $GITHUB_OUTPUT - echo "skip=false" >> $GITHUB_OUTPUT - fi - - - name: Create tag and release - if: steps.version.outputs.skip == 'false' - run: | - TAG=${{ steps.version.outputs.tag }} - git tag ${TAG} - git push origin ${TAG} - gh release create ${TAG} \ - --title "${TAG}" \ - --generate-notes - env: - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} - - uses: actions/setup-python@v5 - if: steps.version.outputs.skip == 'false' with: - python-version: "3.x" + python-version: "3.12" - - name: Deploy versioned docs - if: steps.version.outputs.skip == 'false' + - name: Update Homebrew formula + env: + TAG: ${{ github.event.release.tag_name }} + HOMEBREW_TAP_TOKEN: ${{ secrets.HOMEBREW_TAP_TOKEN }} run: | - pip install "mkdocs==1.6.1" "mkdocs-material==9.7.5" "mike==2.1.4" - git config user.name "github-actions[bot]" - git config user.email "github-actions[bot]@users.noreply.github.com" - TAG=${{ steps.version.outputs.tag }} - bash .github/assemble-docs.sh "$TAG" - mike deploy ${TAG} latest --update-aliases --push - mike set-default latest --push + python3 - <<'SCRIPT' + import hashlib, os, pathlib, subprocess, time, urllib.request - git checkout gh-pages - echo "docs.cactuscompute.com" > CNAME - git add CNAME - git commit -m "Preserve CNAME for custom domain" --allow-empty || true - git push origin gh-pages - git checkout - + tag = os.environ["TAG"] + token = os.environ["HOMEBREW_TAP_TOKEN"] + version = tag.lstrip("v") + ".0" + url = f"https://files.pythonhosted.org/packages/source/c/cactus-compute/cactus_compute-{version}.tar.gz" - - name: Get tarball SHA256 - if: steps.version.outputs.skip == 'false' - id: sha - run: | - TAG=${{ steps.version.outputs.tag }} - URL="https://github.com/${{ github.repository }}/archive/refs/tags/${TAG}.tar.gz" - SHA=$(curl -sL "$URL" | shasum -a 256 | awk '{print $1}') - echo "sha256=${SHA}" >> $GITHUB_OUTPUT - echo "url=${URL}" >> $GITHUB_OUTPUT + for attempt in range(5): + try: + data = urllib.request.urlopen(url, timeout=30).read() + sha = hashlib.sha256(data).hexdigest() + break + except Exception: + print(f"Waiting for PyPI CDN (attempt {attempt + 1})...") + time.sleep(15) + else: + raise RuntimeError(f"Package not available at {url}") - - name: Update Homebrew formula - if: steps.version.outputs.skip == 'false' - run: | - TAG=${{ steps.version.outputs.tag }} - SHA=${{ steps.sha.outputs.sha256 }} - URL=${{ steps.sha.outputs.url }} + formula = f'''class Cactus < Formula + include Language::Python::Virtualenv + desc "On-device AI inference" + homepage "https://cactuscompute.com" + url "{url}" + sha256 "{sha}" + license "Cactus Compute License" + depends_on "python@3.12" + def install + virtualenv_install_with_resources + end + test do + system bin/"cactus", "--help" + end + end + ''' - git clone https://x-access-token:${HOMEBREW_TAP_TOKEN}@github.com/cactus-compute/homebrew-cactus.git tap - cd tap + subprocess.run(["git", "clone", f"https://x-access-token:{token}@github.com/cactus-compute/homebrew-cactus.git", "tap"], check=True) + os.chdir("tap") + pathlib.Path("Formula/cactus.rb").write_text(formula) + subprocess.run(["git", "config", "user.name", "github-actions[bot]"], check=True) + subprocess.run(["git", "config", "user.email", "github-actions[bot]@users.noreply.github.com"], check=True) + subprocess.run(["git", "add", "Formula/cactus.rb"], check=True) + subprocess.run(["git", "commit", "-m", f"Update cactus to {tag}"], check=True) + subprocess.run(["git", "push", "origin", "main"], check=True) + SCRIPT - sed -i "s|url \".*\"|url \"${URL}\"|" Formula/cactus.rb - sed -i "s|sha256 \".*\"|sha256 \"${SHA}\"|" Formula/cactus.rb - - git config user.name "github-actions[bot]" - git config user.email "github-actions[bot]@users.noreply.github.com" - git add Formula/cactus.rb - git commit -m "Update cactus to ${TAG}" - git push origin main - env: - HOMEBREW_TAP_TOKEN: ${{ secrets.HOMEBREW_TAP_TOKEN }} + docs: + permissions: + contents: write + uses: ./.github/workflows/docs.yml + with: + version: ${{ github.event.release.tag_name }} diff --git a/.github/workflows/verify-pr.yml b/.github/workflows/verify-pr.yml deleted file mode 100644 index 1f5e5e9fb..000000000 --- a/.github/workflows/verify-pr.yml +++ /dev/null @@ -1,222 +0,0 @@ -name: PR Verification -# SETUP -# 1. System packages: -# sudo apt-get install -y build-essential libcurl4-openssl-dev cmake libicu-dev -# 2. Python 3.12 via Homebrew (deadsnakes PPA doesn't support Ubuntu 25.10): -# # Install Homebrew -# NONINTERACTIVE=1 /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" - -# # Install Python 3.12 -# eval "$(/home/linuxbrew/.linuxbrew/bin/brew shellenv)" -# brew install python@3.12 - -# # Make it available system-wide -# sudo ln -sf /home/linuxbrew/.linuxbrew/bin/python3.12 /usr/local/bin/python3.12 - -# GitHub Actions Runner Installation (already done) - -# mkdir -p ~/actions-runner && cd ~/actions-runner -# curl -sO -L https://github.com/actions/runner/releases/download/v2.331.0/actions-runner-linux-arm64-2.331.0.tar.gz -# tar xzf actions-runner-linux-arm64-2.331.0.tar.gz -# sudo ./bin/installdependencies.sh - -# Configure Runner for a Repo (repeat this for each repo) - -# 1. Remove old runner config (if switching repos): -# cd ~/actions-runner -# sudo ./svc.sh stop -# sudo ./svc.sh uninstall -# ./config.sh remove --token -# 1. Get the remove token from the old repo: Settings → Actions → Runners → click runner → Remove. -# 2. Get new token: Go to the target repo (e.g. cactus-compute/cactus) → Settings → Actions → Runners → New self-hosted runner → Linux / ARM64. Copy the token from the ./config.sh command -# shown. -# 3. Configure: -# cd ~/actions-runner -# ./config.sh --url https://github.com/cactus-compute/cactus --token -# 3. Accept defaults for runner group, name, and labels (it auto-detects self-hosted, linux, ARM64). -# 4. Start as service: -# sudo ./svc.sh install -# sudo ./svc.sh start -# 5. Add repo secret: Go to the target repo → Settings → Secrets and variables → Actions → New repository secret → Name: HF_TOKEN, Value: -# 6. Verify: The runner should appear as "Online" in the repo's Settings → Actions → Runners. Trigger the workflow from Actions → PR Verification → Run workflow. - -on: - pull_request_review: - types: [submitted] - workflow_dispatch: - -jobs: - gate: - runs-on: ubuntu-latest - outputs: - approved: ${{ steps.check.outputs.approved }} - steps: - - id: check - run: | - if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then - echo "approved=true" >> $GITHUB_OUTPUT - elif [ "${{ github.event.review.state }}" = "approved" ]; then - echo "approved=true" >> $GITHUB_OUTPUT - else - echo "approved=false" >> $GITHUB_OUTPUT - fi - - build: - needs: gate - if: needs.gate.outputs.approved == 'true' - runs-on: [self-hosted, linux, arm64] - steps: - - uses: actions/checkout@v4 - with: - clean: false - - - name: Check for Python changes - id: check_python - env: - GH_TOKEN: ${{ github.token }} - run: | - if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then - echo "python_changed=false" >> $GITHUB_OUTPUT - else - CHANGED=$(gh pr diff ${{ github.event.pull_request.number }} --name-only | grep -cE '^python/(cactus/|requirements\.txt|pyproject\.toml)' || true) - echo "python_changed=$([[ $CHANGED -gt 0 ]] && echo true || echo false)" >> $GITHUB_OUTPUT - fi - - - name: Ensure venv exists - run: | - if [ ! -f venv/bin/activate ]; then - echo "No venv found, running setup..." - source ./setup - fi - - - name: Rebuild venv if Python sources changed - if: steps.check_python.outputs.python_changed == 'true' - run: | - deactivate || true - rm -rf venv - source ./setup - - - name: Purge cached weights if converters changed - if: steps.check_python.outputs.python_changed == 'true' - run: rm -rf weights/ - - - name: Convert models if weights missing - env: - HF_TOKEN: ${{ secrets.HF_TOKEN }} - run: | - source venv/bin/activate - convert_if_missing() { - local model_id=$1 output_dir=$2 bits=$3 - if [ ! -f "weights/$output_dir/config.txt" ]; then - echo "Converting $model_id -> weights/$output_dir" - python3 -m cactus convert "$model_id" "weights/$output_dir" --bits "$bits" ${HF_TOKEN:+--token "$HF_TOKEN"} || echo "WARNING: conversion of $model_id failed (exit $?), checking fallback directory" - local default_dir="weights/$(echo "$model_id" | sed 's|.*/||' | tr '[:upper:]' '[:lower:]')" - if [ ! -f "weights/$output_dir/config.txt" ] && [ -f "$default_dir/config.txt" ]; then - mv "$default_dir" "weights/$output_dir" - fi - fi - } - convert_if_missing "Qwen/Qwen3-0.6B" "qwen3-0.6b" "4" - convert_if_missing "nomic-ai/nomic-embed-text-v2-moe" "nomic-embed-text-v2-moe" "4" - convert_if_missing "LiquidAI/LFM2.5-350M" "lfm2-350m" "4" - convert_if_missing "LiquidAI/LFM2-8B-A1B" "lfm2-8b-a1b" "4" - convert_if_missing "google/gemma-3-270m-it" "gemma-3-270m-it" "4" - convert_if_missing "openai/whisper-small" "whisper-small" "4" - convert_if_missing "UsefulSensors/moonshine-base" "moonshine-base" "4" - convert_if_missing "LiquidAI/LFM2-VL-450M" "lfm2-vl-450m-int8" "4" - convert_if_missing "LiquidAI/LFM2-VL-450M" "lfm2-vl-450m-int4" "4" - convert_if_missing "LiquidAI/LFM2-VL-450M" "lfm2-vl-450m-fp16" "4" - - - name: Build library - run: bash cactus/build.sh - - - name: Build tests - run: | - cd cactus-engine/tests - rm -rf build && mkdir -p build && cd build - cmake .. -DCMAKE_RULE_MESSAGES=OFF -DCMAKE_VERBOSE_MAKEFILE=OFF - make -j$(nproc) - - unit-tests: - needs: build - runs-on: [self-hosted, linux, arm64] - steps: - - uses: actions/checkout@v4 - with: - clean: false - - name: Run unit tests - env: - CACTUS_INDEX_PATH: ${{ runner.temp }}/cactus_index - CACTUS_NO_CLOUD_TELE: "1" - run: | - mkdir -p "$CACTUS_INDEX_PATH" - FAILED=0 - for test in test_graph test_kernel test_kv_cache test_index test_model_loading; do - echo "=== Running $test ===" - if ! ./cactus-engine/tests/build/$test; then - FAILED=$((FAILED + 1)) - fi - done - if [ $FAILED -gt 0 ]; then exit 1; fi - - exhaustive-tests: - needs: build - runs-on: [self-hosted, linux, arm64] - strategy: - fail-fast: false - matrix: - include: - - family: qwen - model_dir: qwen3-0.6b - env_var: CACTUS_TEST_MODEL - precision: INT8 - - family: nomic - model_dir: nomic-embed-text-v2-moe - env_var: CACTUS_TEST_MODEL - precision: INT8 - - family: lfm2 - model_dir: lfm2-350m - env_var: CACTUS_TEST_MODEL - precision: INT8 - - family: lfm2moe - model_dir: lfm2-8b-a1b - env_var: CACTUS_TEST_MODEL - precision: INT8 - - family: gemma - model_dir: gemma-3-270m-it - env_var: CACTUS_TEST_MODEL - precision: INT8 - - family: whisper - model_dir: whisper-small - env_var: CACTUS_TEST_TRANSCRIBE_MODEL - precision: INT8 - - family: moonshine - model_dir: moonshine-base - env_var: CACTUS_TEST_TRANSCRIBE_MODEL - precision: INT8 - - family: lfm2vl - model_dir: lfm2-vl-450m-int8 - env_var: CACTUS_TEST_MODEL - precision: INT8 - - family: lfm2vl - model_dir: lfm2-vl-450m-int4 - env_var: CACTUS_TEST_MODEL - precision: INT4 - - family: lfm2vl - model_dir: lfm2-vl-450m-fp16 - env_var: CACTUS_TEST_MODEL - precision: FP16 - steps: - - uses: actions/checkout@v4 - with: - clean: false - - name: Run exhaustive test - env: - CACTUS_NO_CLOUD_TELE: "1" - run: | - export ${{ matrix.env_var }}="${{ github.workspace }}/weights/${{ matrix.model_dir }}" - export CACTUS_TEST_ASSETS="${{ github.workspace }}/cactus-engine/tests/assets" - export CACTUS_TEST_GOLDEN_FILE="${{ github.workspace }}/cactus-engine/tests/golden/golden.json" - export CACTUS_TEST_GOLDEN_FAMILY="${{ matrix.family }}" - export CACTUS_TEST_GOLDEN_PRECISION="${{ matrix.precision }}" - ./cactus-engine/tests/build/test_exhaustive diff --git a/CACTUS_VERSION b/CACTUS_VERSION index e0c4adc50..c0ec837a4 100644 --- a/CACTUS_VERSION +++ b/CACTUS_VERSION @@ -1 +1 @@ -v1.14 \ No newline at end of file +v2.0 diff --git a/README.md b/README.md index 9c25eaaf3..f52922737 100644 --- a/README.md +++ b/README.md @@ -120,7 +120,7 @@ graph.hard_reset(); | Reference | Language | Description | |-----------|----------|-------------| -| Engine API | C | Chat completion, streaming, tool calling, transcription, embeddings, RAG, vision, VAD, vector index, cloud handoff | +| Engine API | C | Chat completion, streaming, tool calling, transcription, embeddings, RAG, vision, vector index, cloud handoff | | Graph API | C++ | Tensor operations, matrix multiplication, attention, normalization, activation functions | | [Python Package](/python/) | Python | Python package and CLI | diff --git a/android/CMakeLists.txt b/android/CMakeLists.txt index 79b424cab..d04de1768 100644 --- a/android/CMakeLists.txt +++ b/android/CMakeLists.txt @@ -1,75 +1,62 @@ cmake_minimum_required(VERSION 3.10) project(CactusAndroid LANGUAGES CXX) -set(CMAKE_CXX_STANDARD 20) -set(CMAKE_CXX_STANDARD_REQUIRED True) - -set(PROJECT_ROOT "${CMAKE_SOURCE_DIR}/..") - -if(NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE Release) -endif() +set(CMAKE_BUILD_TYPE Release CACHE STRING "") +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) if(NOT ${ANDROID_ABI} STREQUAL "arm64-v8a") message(FATAL_ERROR "Unsupported Android ABI: ${ANDROID_ABI}. Cactus only supports arm64-v8a.") endif() -set(CACTUS_MBEDTLS_ROOT "${CMAKE_SOURCE_DIR}/mbedtls/${ANDROID_ABI}") +set(CACTUS_MBEDTLS_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/mbedtls/${ANDROID_ABI}") if(NOT EXISTS "${CACTUS_MBEDTLS_ROOT}") - set(CACTUS_MBEDTLS_ROOT "${PROJECT_ROOT}/libs/mbedtls/android/${ANDROID_ABI}") + set(CACTUS_MBEDTLS_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../libs/mbedtls/android/${ANDROID_ABI}") endif() -set(CACTUS_MBEDTLS_LIBRARY "${CACTUS_MBEDTLS_ROOT}/lib/libmbedtls.a") -set(CACTUS_MBEDX509_LIBRARY "${CACTUS_MBEDTLS_ROOT}/lib/libmbedx509.a") -set(CACTUS_MBEDCRYPTO_LIBRARY "${CACTUS_MBEDTLS_ROOT}/lib/libmbedcrypto.a") -set(CACTUS_ENGINE_DIR "${PROJECT_ROOT}/cactus-engine") if(NOT TARGET cactus_engine) - add_subdirectory(${CACTUS_ENGINE_DIR} ${CMAKE_CURRENT_BINARY_DIR}/cactus-engine) + add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../cactus ${CMAKE_CURRENT_BINARY_DIR}/cactus) endif() +set(_DEPS cactus_engine cactus_graph cactus_kernels) + +add_library(cactus_jni SHARED ${CMAKE_CURRENT_SOURCE_DIR}/cactus_jni.cpp) + +set_target_properties(cactus_jni PROPERTIES + OUTPUT_NAME cactus + CXX_STANDARD 20 + CXX_STANDARD_REQUIRED ON +) + +target_include_directories(cactus_jni PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/../cactus + ${CMAKE_CURRENT_SOURCE_DIR}/../cactus-engine + ${CMAKE_CURRENT_SOURCE_DIR}/../cactus-engine/src +) + +target_link_libraries(cactus_jni PRIVATE ${_DEPS}) + find_library(LOG_LIB log) +target_link_libraries(cactus_jni PRIVATE ${LOG_LIB} android) -add_library(cactus SHARED ${CMAKE_SOURCE_DIR}/cactus_jni.cpp) -target_include_directories(cactus PRIVATE ${PROJECT_ROOT}/cactus ${CACTUS_ENGINE_DIR} ${CACTUS_ENGINE_DIR}/src) -target_link_libraries(cactus PRIVATE cactus_engine cactus_graph cactus_kernels) -target_link_libraries(cactus PRIVATE ${LOG_LIB} android) -if(EXISTS "${CACTUS_MBEDTLS_LIBRARY}" AND EXISTS "${CACTUS_MBEDX509_LIBRARY}" AND EXISTS "${CACTUS_MBEDCRYPTO_LIBRARY}") - target_link_libraries(cactus PRIVATE - "${CACTUS_MBEDTLS_LIBRARY}" - "${CACTUS_MBEDX509_LIBRARY}" - "${CACTUS_MBEDCRYPTO_LIBRARY}" - ) +set(_MBEDTLS "${CACTUS_MBEDTLS_ROOT}/lib/libmbedtls.a") +set(_MBEDX509 "${CACTUS_MBEDTLS_ROOT}/lib/libmbedx509.a") +set(_MBEDCRYPTO "${CACTUS_MBEDTLS_ROOT}/lib/libmbedcrypto.a") +if(EXISTS "${_MBEDTLS}" AND EXISTS "${_MBEDX509}" AND EXISTS "${_MBEDCRYPTO}") + target_link_libraries(cactus_jni PRIVATE "${_MBEDTLS}" "${_MBEDX509}" "${_MBEDCRYPTO}") endif() -target_compile_definitions(cactus PRIVATE PLATFORM_CPU_ONLY=1) -set(_CACTUS_VERSION_FILE "${PROJECT_ROOT}/CACTUS_VERSION") +target_compile_definitions(cactus_jni PRIVATE PLATFORM_CPU_ONLY=1) + +set(_CACTUS_VERSION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/../CACTUS_VERSION") if(EXISTS "${_CACTUS_VERSION_FILE}") file(READ "${_CACTUS_VERSION_FILE}" _CACTUS_VERSION_CONTENT) string(STRIP "${_CACTUS_VERSION_CONTENT}" CACTUS_VERSION_STR) - target_compile_definitions(cactus PRIVATE CACTUS_COMPILE_TIME_VERSION="${CACTUS_VERSION_STR}") + target_compile_definitions(cactus_jni PRIVATE CACTUS_COMPILE_TIME_VERSION="${CACTUS_VERSION_STR}") endif() if(CMAKE_BUILD_TYPE STREQUAL "Debug") - target_compile_options(cactus PRIVATE -DCACTUS_ANDROID_ENABLE_LOGGING) + target_compile_definitions(cactus_jni PRIVATE CACTUS_ANDROID_ENABLE_LOGGING) endif() -target_compile_options(cactus PRIVATE -fvisibility=hidden -fvisibility-inlines-hidden) -target_link_options(cactus PRIVATE -Wl,--gc-sections -Wl,--exclude-libs,ALL -flto -Wl,-z,max-page-size=16384) - -set(MRI_SCRIPT "${CMAKE_BINARY_DIR}/combine.mri") -file(GENERATE OUTPUT ${MRI_SCRIPT} CONTENT -"create ${CMAKE_BINARY_DIR}/lib/libcactus_static.a -addlib $ -addlib $ -addlib $ -save -end -") -add_custom_target(cactus_static ALL - COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_BINARY_DIR}/lib - COMMAND ${CMAKE_AR} -M < ${MRI_SCRIPT} - DEPENDS cactus_engine cactus_graph cactus_kernels - COMMENT "Merging static libraries into libcactus_static.a" -) - -set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) +target_compile_options(cactus_jni PRIVATE -fvisibility=hidden -fvisibility-inlines-hidden -ffunction-sections -fdata-sections) +target_link_options(cactus_jni PRIVATE -Wl,--gc-sections -Wl,--exclude-libs,ALL -flto -Wl,-z,max-page-size=16384) diff --git a/android/cactus_jni.cpp b/android/cactus_jni.cpp index f9618c3d4..7386753f8 100644 --- a/android/cactus_jni.cpp +++ b/android/cactus_jni.cpp @@ -330,110 +330,6 @@ Java_com_cactus_CactusJNI_nativeAudioEmbed(JNIEnv* env, jobject, jlong handle, return result; } -JNIEXPORT jint JNICALL -Java_com_cactus_CactusJNI_nativeVad(JNIEnv* env, jobject, jlong handle, - jstring audioPath, jbyteArray responseBuffer, - jstring optionsJson, jbyteArray pcmData) { - const char* path = audioPath ? env->GetStringUTFChars(audioPath, nullptr) : nullptr; - const char* options = optionsJson ? env->GetStringUTFChars(optionsJson, nullptr) : nullptr; - - jsize bufSize = env->GetArrayLength(responseBuffer); - jbyte* buf = env->GetByteArrayElements(responseBuffer, nullptr); - - jbyte* pcmBytes = nullptr; - size_t pcmSize = 0; - if (pcmData) { - pcmSize = env->GetArrayLength(pcmData); - pcmBytes = env->GetByteArrayElements(pcmData, nullptr); - } - - int result = cactus_vad( - reinterpret_cast(handle), - path, reinterpret_cast(buf), static_cast(bufSize), - options, - reinterpret_cast(pcmBytes), pcmSize - ); - - env->ReleaseByteArrayElements(responseBuffer, buf, 0); - if (pcmBytes) env->ReleaseByteArrayElements(pcmData, pcmBytes, JNI_ABORT); - if (path) env->ReleaseStringUTFChars(audioPath, path); - if (options) env->ReleaseStringUTFChars(optionsJson, options); - - return result; -} - -JNIEXPORT jint JNICALL -Java_com_cactus_CactusJNI_nativeDiarize(JNIEnv* env, jobject, jlong handle, - jstring audioPath, jbyteArray responseBuffer, - jstring optionsJson, jbyteArray pcmData) { - const char* path = audioPath ? env->GetStringUTFChars(audioPath, nullptr) : nullptr; - const char* options = optionsJson ? env->GetStringUTFChars(optionsJson, nullptr) : nullptr; - - jsize bufSize = env->GetArrayLength(responseBuffer); - jbyte* buf = env->GetByteArrayElements(responseBuffer, nullptr); - - jbyte* pcmBytes = nullptr; - size_t pcmSize = 0; - if (pcmData) { - pcmSize = env->GetArrayLength(pcmData); - pcmBytes = env->GetByteArrayElements(pcmData, nullptr); - } - - int result = cactus_diarize( - reinterpret_cast(handle), - path, reinterpret_cast(buf), static_cast(bufSize), - options, - reinterpret_cast(pcmBytes), pcmSize - ); - - env->ReleaseByteArrayElements(responseBuffer, buf, 0); - if (pcmBytes) env->ReleaseByteArrayElements(pcmData, pcmBytes, JNI_ABORT); - if (path) env->ReleaseStringUTFChars(audioPath, path); - if (options) env->ReleaseStringUTFChars(optionsJson, options); - - return result; -} - -JNIEXPORT jint JNICALL -Java_com_cactus_CactusJNI_nativeEmbedSpeaker(JNIEnv* env, jobject, jlong handle, - jstring audioPath, jbyteArray responseBuffer, - jstring optionsJson, jbyteArray pcmData, - jfloatArray maskWeights, jlong maskNumFrames) { - const char* path = audioPath ? env->GetStringUTFChars(audioPath, nullptr) : nullptr; - const char* options = optionsJson ? env->GetStringUTFChars(optionsJson, nullptr) : nullptr; - - jsize bufSize = env->GetArrayLength(responseBuffer); - jbyte* buf = env->GetByteArrayElements(responseBuffer, nullptr); - - jbyte* pcmBytes = nullptr; - size_t pcmSize = 0; - if (pcmData) { - pcmSize = env->GetArrayLength(pcmData); - pcmBytes = env->GetByteArrayElements(pcmData, nullptr); - } - - jfloat* maskPtr = nullptr; - if (maskWeights) { - maskPtr = env->GetFloatArrayElements(maskWeights, nullptr); - } - - int result = cactus_embed_speaker( - reinterpret_cast(handle), - path, reinterpret_cast(buf), static_cast(bufSize), - options, - reinterpret_cast(pcmBytes), pcmSize, - maskPtr, static_cast(maskNumFrames) - ); - - env->ReleaseByteArrayElements(responseBuffer, buf, 0); - if (maskPtr) env->ReleaseFloatArrayElements(maskWeights, maskPtr, JNI_ABORT); - if (pcmBytes) env->ReleaseByteArrayElements(pcmData, pcmBytes, JNI_ABORT); - if (path) env->ReleaseStringUTFChars(audioPath, path); - if (options) env->ReleaseStringUTFChars(optionsJson, options); - - return result; -} - JNIEXPORT jint JNICALL Java_com_cactus_CactusJNI_nativeRagQuery(JNIEnv* env, jobject, jlong handle, jstring query, jbyteArray responseBuffer, jlong topK) { diff --git a/apple/CMakeLists.txt b/apple/CMakeLists.txt index 531e253c8..510dfa64a 100644 --- a/apple/CMakeLists.txt +++ b/apple/CMakeLists.txt @@ -1,91 +1,66 @@ cmake_minimum_required(VERSION 3.10) project(CactusApple LANGUAGES CXX) -set(CMAKE_CXX_STANDARD 20) -set(CMAKE_CXX_STANDARD_REQUIRED True) - -set(CMAKE_OSX_DEPLOYMENT_TARGET "13.0" CACHE STRING "Minimum macOS version") -set(CMAKE_XCODE_ATTRIBUTE_IPHONEOS_DEPLOYMENT_TARGET "13.0" CACHE STRING "Minimum iOS version") - -set(PROJECT_ROOT "${CMAKE_SOURCE_DIR}/..") - -if(NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE Release) -endif() +set(CMAKE_OSX_DEPLOYMENT_TARGET "13.0" CACHE STRING "") +set(CMAKE_XCODE_ATTRIBUTE_IPHONEOS_DEPLOYMENT_TARGET "13.0" CACHE STRING "") +set(CMAKE_BUILD_TYPE Release CACHE STRING "") +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) -set(CACTUS_ENGINE_DIR "${PROJECT_ROOT}/cactus-engine") if(NOT TARGET cactus_engine) - add_subdirectory(${CACTUS_ENGINE_DIR} ${CMAKE_CURRENT_BINARY_DIR}/cactus-engine) + add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../cactus ${CMAKE_CURRENT_BINARY_DIR}/cactus) endif() +set(_DEPS cactus_engine cactus_graph cactus_kernels) + if(BUILD_SHARED_LIBS) - file(WRITE "${CMAKE_CURRENT_BINARY_DIR}/empty.cpp" "") - add_library(cactus SHARED "${CMAKE_CURRENT_BINARY_DIR}/empty.cpp") - add_dependencies(cactus cactus_engine cactus_graph cactus_kernels) + file(WRITE ${CMAKE_CURRENT_BINARY_DIR}/stub.cpp "") + add_library(cactus_framework SHARED ${CMAKE_CURRENT_BINARY_DIR}/stub.cpp) + set_target_properties(cactus_framework PROPERTIES OUTPUT_NAME cactus) + add_dependencies(cactus_framework ${_DEPS}) - target_link_options(cactus PRIVATE + target_link_options(cactus_framework PRIVATE -force_load $ -force_load $ -force_load $ + -Wl,-dead_strip -flto ) - find_library(COREML_FRAMEWORK CoreML REQUIRED) - find_library(FOUNDATION_FRAMEWORK Foundation REQUIRED) - find_library(ACCELERATE_FRAMEWORK Accelerate REQUIRED) - find_library(SECURITY_FRAMEWORK Security REQUIRED) - find_library(SYSTEMCONFIGURATION_FRAMEWORK SystemConfiguration REQUIRED) - find_library(CFNETWORK_FRAMEWORK CFNetwork REQUIRED) - target_link_libraries(cactus PUBLIC - ${COREML_FRAMEWORK} - ${FOUNDATION_FRAMEWORK} - ${ACCELERATE_FRAMEWORK} - ${SECURITY_FRAMEWORK} - ${SYSTEMCONFIGURATION_FRAMEWORK} - ${CFNETWORK_FRAMEWORK} + target_link_libraries(cactus_framework PUBLIC + "-framework Accelerate" "-framework CoreML" "-framework Foundation" + "-framework Security" "-framework SystemConfiguration" "-framework CFNetwork" ) - set(CACTUS_CURL_INCLUDE_DIR "${CACTUS_CURL_ROOT}/include") if(CMAKE_SYSTEM_NAME STREQUAL "iOS") string(TOLOWER "${CMAKE_OSX_SYSROOT}" _sysroot_lower) if(_sysroot_lower MATCHES "simulator") - set(CACTUS_CURL_LIBRARY "${CACTUS_CURL_ROOT}/ios/simulator/libcurl.a") + set(_CURL "${CACTUS_CURL_ROOT}/ios/simulator/libcurl.a") else() - set(CACTUS_CURL_LIBRARY "${CACTUS_CURL_ROOT}/ios/device/libcurl.a") + set(_CURL "${CACTUS_CURL_ROOT}/ios/device/libcurl.a") endif() else() - set(CACTUS_CURL_LIBRARY "${CACTUS_CURL_ROOT}/macos/libcurl.a") + set(_CURL "${CACTUS_CURL_ROOT}/macos/libcurl.a") endif() - if(EXISTS "${CACTUS_CURL_INCLUDE_DIR}/curl/curl.h" AND EXISTS "${CACTUS_CURL_LIBRARY}") - target_link_libraries(cactus PUBLIC "${CACTUS_CURL_LIBRARY}") - target_include_directories(cactus PUBLIC "${CACTUS_CURL_INCLUDE_DIR}") + if(EXISTS "${_CURL}") + target_link_libraries(cactus_framework PUBLIC "${_CURL}") endif() - set_target_properties(cactus PROPERTIES + set_target_properties(cactus_framework PROPERTIES FRAMEWORK TRUE FRAMEWORK_VERSION A MACOSX_FRAMEWORK_IDENTIFIER com.cactuscompute.cactus VERSION 1.0.0 SOVERSION 1.0.0 - PUBLIC_HEADER "${CACTUS_ENGINE_DIR}/cactus_engine.h" - MACOSX_FRAMEWORK_INFO_PLIST "${CMAKE_SOURCE_DIR}/Info.plist" + MACOSX_FRAMEWORK_INFO_PLIST "${CMAKE_CURRENT_SOURCE_DIR}/Info.plist" + ) + + add_custom_command(TARGET cactus_framework POST_BUILD + COMMAND ${CMAKE_COMMAND} -E make_directory "$/Headers" + COMMAND ${CMAKE_COMMAND} -E copy + "${CMAKE_CURRENT_SOURCE_DIR}/../cactus-engine/cactus_engine.h" + "$/Headers" ) if(CMAKE_SYSTEM_NAME STREQUAL "iOS") - target_compile_definitions(cactus PRIVATE TARGET_OS_IPHONE=1) + target_compile_definitions(cactus_framework PRIVATE TARGET_OS_IPHONE=1) endif() - - target_link_options(cactus PRIVATE -Wl,-dead_strip -flto) -else() - add_custom_target(cactus ALL - COMMAND libtool -static -o ${CMAKE_BINARY_DIR}/libcactus.a - $ - $ - $ - DEPENDS cactus_engine cactus_graph cactus_kernels - COMMENT "Merging static libraries into libcactus.a" - ) endif() - -set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) -set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib) -set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin) diff --git a/apple/build.sh b/apple/build.sh index 8fad949c2..fddffaea7 100755 --- a/apple/build.sh +++ b/apple/build.sh @@ -31,9 +31,7 @@ echo "Vendored libcurl root: $CACTUS_CURL_ROOT" function cp_headers() { mkdir -p "$ROOT_DIR/apple/$1/$2/cactus.framework/Headers" - cp "$ROOT_DIR/cactus-engine/cactus_engine.h" "$ROOT_DIR/apple/$1/$2/cactus.framework/Headers/" 2>/dev/null || true - cp "$ROOT_DIR/cactus-graph/cactus_graph.h" "$ROOT_DIR/apple/$1/$2/cactus.framework/Headers/" 2>/dev/null || true - cp "$ROOT_DIR/cactus-kernels/cactus_kernels.h" "$ROOT_DIR/apple/$1/$2/cactus.framework/Headers/" 2>/dev/null || true + cp "$ROOT_DIR/cactus-engine/cactus_engine.h" "$ROOT_DIR/apple/$1/$2/cactus.framework/Headers/" } function create_ios_xcframework_info_plist() { diff --git a/bindings/flutter/cactus.dart b/bindings/flutter/cactus.dart index a1689b2bd..11ba4f1e8 100644 --- a/bindings/flutter/cactus.dart +++ b/bindings/flutter/cactus.dart @@ -195,68 +195,6 @@ final cactusAudioEmbed = _lib.lookupFunction<_CactusAudioEmbedN, _CactusAudioEmbedD>( 'cactus_audio_embed'); -typedef _CactusVadN = Int32 Function( - Pointer model, - Pointer audioFilePath, - Pointer responseBuffer, - IntPtr bufferSize, - Pointer optionsJson, - Pointer pcmBuffer, - IntPtr pcmBufferSize); -typedef _CactusVadD = int Function( - Pointer model, - Pointer audioFilePath, - Pointer responseBuffer, - int bufferSize, - Pointer optionsJson, - Pointer pcmBuffer, - int pcmBufferSize); -final cactusVad = - _lib.lookupFunction<_CactusVadN, _CactusVadD>('cactus_vad'); - -typedef _CactusDiarizeN = Int32 Function( - Pointer model, - Pointer audioFilePath, - Pointer responseBuffer, - IntPtr bufferSize, - Pointer optionsJson, - Pointer pcmBuffer, - IntPtr pcmBufferSize); -typedef _CactusDiarizeD = int Function( - Pointer model, - Pointer audioFilePath, - Pointer responseBuffer, - int bufferSize, - Pointer optionsJson, - Pointer pcmBuffer, - int pcmBufferSize); -final cactusDiarize = - _lib.lookupFunction<_CactusDiarizeN, _CactusDiarizeD>('cactus_diarize'); - -typedef _CactusEmbedSpeakerN = Int32 Function( - Pointer model, - Pointer audioFilePath, - Pointer responseBuffer, - IntPtr bufferSize, - Pointer optionsJson, - Pointer pcmBuffer, - IntPtr pcmBufferSize, - Pointer maskWeights, - IntPtr maskNumFrames); -typedef _CactusEmbedSpeakerD = int Function( - Pointer model, - Pointer audioFilePath, - Pointer responseBuffer, - int bufferSize, - Pointer optionsJson, - Pointer pcmBuffer, - int pcmBufferSize, - Pointer maskWeights, - int maskNumFrames); -final cactusEmbedSpeaker = - _lib.lookupFunction<_CactusEmbedSpeakerN, _CactusEmbedSpeakerD>( - 'cactus_embed_speaker'); - typedef _CactusRagQueryN = Int32 Function( Pointer model, Pointer query, diff --git a/bindings/kotlin/Cactus.android.kt b/bindings/kotlin/Cactus.android.kt index 7e2c8986b..553a81f55 100644 --- a/bindings/kotlin/Cactus.android.kt +++ b/bindings/kotlin/Cactus.android.kt @@ -66,24 +66,6 @@ actual fun cactusAudioEmbed(handle: Long, audioPath: String): FloatArray { return buffer.copyOf(outDim[0].toInt()) } -actual fun cactusVad(handle: Long, audioPath: String?, optionsJson: String?, pcmData: ByteArray?): String { - val buffer = ByteArray(1024 * 1024) - check(CactusJNI.nativeVad(handle, audioPath, buffer, optionsJson, pcmData)) - return buffer.decodeToString().trimEnd('\u0000') -} - -actual fun cactusDiarize(handle: Long, audioPath: String?, optionsJson: String?, pcmData: ByteArray?): String { - val buffer = ByteArray(1024 * 1024) - check(CactusJNI.nativeDiarize(handle, audioPath, buffer, optionsJson, pcmData)) - return buffer.decodeToString().trimEnd('\u0000') -} - -actual fun cactusEmbedSpeaker(handle: Long, audioPath: String?, optionsJson: String?, pcmData: ByteArray?, maskWeights: FloatArray?, maskNumFrames: Long): String { - val buffer = ByteArray(1024 * 1024) - check(CactusJNI.nativeEmbedSpeaker(handle, audioPath, buffer, optionsJson, pcmData, maskWeights, maskNumFrames)) - return buffer.decodeToString().trimEnd('\u0000') -} - actual fun cactusRagQuery(handle: Long, query: String, topK: Long): String { val buffer = ByteArray(1024 * 1024) check(CactusJNI.nativeRagQuery(handle, query, buffer, topK)) diff --git a/bindings/kotlin/Cactus.common.kt b/bindings/kotlin/Cactus.common.kt index f1e88d47a..40f4bc6a3 100644 --- a/bindings/kotlin/Cactus.common.kt +++ b/bindings/kotlin/Cactus.common.kt @@ -1,5 +1,13 @@ package com.cactus +fun interface CactusTokenCallback { + fun onToken(token: String, tokenId: Int) +} + +fun interface CactusLogCallback { + fun onLog(level: Int, component: String, message: String) +} + expect fun cactusInit(modelPath: String, corpusDir: String?, cacheIndex: Boolean): Long expect fun cactusDestroy(handle: Long) expect fun cactusReset(handle: Long) @@ -12,9 +20,6 @@ expect fun cactusTranscribe(handle: Long, audioPath: String?, prompt: String, op expect fun cactusEmbed(handle: Long, text: String, normalize: Boolean): FloatArray expect fun cactusImageEmbed(handle: Long, imagePath: String): FloatArray expect fun cactusAudioEmbed(handle: Long, audioPath: String): FloatArray -expect fun cactusVad(handle: Long, audioPath: String?, optionsJson: String?, pcmData: ByteArray?): String -expect fun cactusDiarize(handle: Long, audioPath: String?, optionsJson: String?, pcmData: ByteArray?): String -expect fun cactusEmbedSpeaker(handle: Long, audioPath: String?, optionsJson: String?, pcmData: ByteArray?, maskWeights: FloatArray?, maskNumFrames: Long): String expect fun cactusRagQuery(handle: Long, query: String, topK: Long): String expect fun cactusIndexInit(indexDir: String, embeddingDim: Long): Long expect fun cactusIndexAdd(handle: Long, ids: IntArray, documents: Array, metadatas: Array?, embeddings: Array, embeddingDim: Long): Int diff --git a/bindings/kotlin/Cactus.ios.kt b/bindings/kotlin/Cactus.ios.kt index 083d1cf09..17f500c8a 100644 --- a/bindings/kotlin/Cactus.ios.kt +++ b/bindings/kotlin/Cactus.ios.kt @@ -167,54 +167,6 @@ actual fun cactusAudioEmbed(handle: Long, audioPath: String): FloatArray { } } -@OptIn(ExperimentalForeignApi::class) -actual fun cactusVad(handle: Long, audioPath: String?, optionsJson: String?, pcmData: ByteArray?): String { - memScoped { - val buffer = allocArray(1048576) - val pcmPtr = pcmData?.refTo(0)?.getPointer(this) - val result = cactus_vad( - handle.toCPointer(), audioPath, buffer, 1048576u, optionsJson, - pcmPtr?.reinterpret(), pcmData?.size?.toULong() ?: 0u - ) - if (result < 0) throw RuntimeException(cactus_get_last_error()?.toKString() ?: "Unknown error") - return buffer.toKString() - } -} - -@OptIn(ExperimentalForeignApi::class) -actual fun cactusDiarize(handle: Long, audioPath: String?, optionsJson: String?, pcmData: ByteArray?): String { - memScoped { - val buffer = allocArray(1048576) - val pcmPtr = pcmData?.refTo(0)?.getPointer(this) - val result = cactus_diarize( - handle.toCPointer(), audioPath, buffer, 1048576u, optionsJson, - pcmPtr?.reinterpret(), pcmData?.size?.toULong() ?: 0u - ) - if (result < 0) throw RuntimeException(cactus_get_last_error()?.toKString() ?: "Unknown error") - return buffer.toKString() - } -} - -@OptIn(ExperimentalForeignApi::class) -actual fun cactusEmbedSpeaker(handle: Long, audioPath: String?, optionsJson: String?, pcmData: ByteArray?, maskWeights: FloatArray?, maskNumFrames: Long): String { - memScoped { - val buffer = allocArray(1048576) - val pcmPtr = pcmData?.refTo(0)?.getPointer(this) - val maskPtr = maskWeights?.let { - val arr = allocArray(it.size) - it.forEachIndexed { i, v -> arr[i] = v } - arr - } - val result = cactus_embed_speaker( - handle.toCPointer(), audioPath, buffer, 1048576u, optionsJson, - pcmPtr?.reinterpret(), pcmData?.size?.toULong() ?: 0u, - maskPtr, maskNumFrames.toULong() - ) - if (result < 0) throw RuntimeException(cactus_get_last_error()?.toKString() ?: "Unknown error") - return buffer.toKString() - } -} - @OptIn(ExperimentalForeignApi::class) actual fun cactusRagQuery(handle: Long, query: String, topK: Long): String { memScoped { diff --git a/bindings/kotlin/Cactus.kt b/bindings/kotlin/Cactus.kt index 76cbe1dac..28e38a773 100644 --- a/bindings/kotlin/Cactus.kt +++ b/bindings/kotlin/Cactus.kt @@ -1,13 +1,5 @@ package com.cactus -fun interface CactusTokenCallback { - fun onToken(token: String, tokenId: Int) -} - -fun interface CactusLogCallback { - fun onLog(level: Int, component: String, message: String) -} - object CactusJNI { init { System.loadLibrary("cactus") @@ -25,9 +17,6 @@ object CactusJNI { @JvmStatic external fun nativeEmbed(handle: Long, text: String, embeddingsBuffer: FloatArray, outEmbeddingDim: LongArray, normalize: Boolean): Int @JvmStatic external fun nativeImageEmbed(handle: Long, imagePath: String, embeddingsBuffer: FloatArray, outEmbeddingDim: LongArray): Int @JvmStatic external fun nativeAudioEmbed(handle: Long, audioPath: String, embeddingsBuffer: FloatArray, outEmbeddingDim: LongArray): Int - @JvmStatic external fun nativeVad(handle: Long, audioPath: String?, responseBuffer: ByteArray, optionsJson: String?, pcmData: ByteArray?): Int - @JvmStatic external fun nativeDiarize(handle: Long, audioPath: String?, responseBuffer: ByteArray, optionsJson: String?, pcmData: ByteArray?): Int - @JvmStatic external fun nativeEmbedSpeaker(handle: Long, audioPath: String?, responseBuffer: ByteArray, optionsJson: String?, pcmData: ByteArray?, maskWeights: FloatArray?, maskNumFrames: Long): Int @JvmStatic external fun nativeRagQuery(handle: Long, query: String, responseBuffer: ByteArray, topK: Long): Int @JvmStatic external fun nativeIndexInit(indexDir: String, embeddingDim: Long): Long @JvmStatic external fun nativeIndexAdd(handle: Long, ids: IntArray, documents: Array, metadatas: Array?, embeddings: Array, embeddingDim: Long): Int diff --git a/bindings/react-native/android/CactusModule.kt b/bindings/react-native/android/CactusModule.kt index eb02ce147..d54834c50 100644 --- a/bindings/react-native/android/CactusModule.kt +++ b/bindings/react-native/android/CactusModule.kt @@ -8,6 +8,8 @@ import com.facebook.react.bridge.ReactApplicationContext import com.facebook.react.bridge.ReactContextBaseJavaModule import com.facebook.react.bridge.ReactMethod import com.facebook.react.bridge.ReadableArray +import com.facebook.react.modules.core.DeviceEventManagerModule +import com.cactus.CactusTokenCallback import org.json.JSONArray import org.json.JSONObject @@ -116,6 +118,22 @@ class CactusModule(reactContext: ReactApplicationContext) : ReactContextBaseJava promise.resolve(decodeNullTerminatedUtf8(buffer)) } + @ReactMethod + private fun emitToken(token: String, tokenId: Int) { + reactApplicationContext + .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter::class.java) + .emit("onToken", Arguments.createMap().apply { + putString("token", token) + putInt("tokenId", tokenId) + }) + } + + @ReactMethod + fun addListener(@Suppress("UNUSED_PARAMETER") eventName: String) {} + + @ReactMethod + fun removeListeners(@Suppress("UNUSED_PARAMETER") count: Int) {} + @ReactMethod fun complete( handle: String, @@ -123,17 +141,19 @@ class CactusModule(reactContext: ReactApplicationContext) : ReactContextBaseJava optionsJson: String?, toolsJson: String?, pcmDataBase64: String?, + streamTokens: Boolean, promise: Promise, ) { val buffer = ByteArray(DEFAULT_BUFFER_SIZE) val nativeHandle = parseHandle(handle, promise) ?: return + val callback = if (streamTokens) CactusTokenCallback { token, tokenId -> emitToken(token, tokenId) } else null val rc = CactusJNI.nativeComplete( nativeHandle, messagesJson, buffer, optionsJson, toolsJson, - null, + callback, decodeBase64OrNull(pcmDataBase64), ) if (rc < 0) { @@ -257,58 +277,6 @@ class CactusModule(reactContext: ReactApplicationContext) : ReactContextBaseJava promise.resolve(result) } - @ReactMethod - fun vad(handle: String, audioPath: String?, optionsJson: String?, pcmDataBase64: String?, promise: Promise) { - val nativeHandle = parseHandle(handle, promise) ?: return - val buffer = ByteArray(DEFAULT_BUFFER_SIZE) - val rc = CactusJNI.nativeVad(nativeHandle, audioPath, buffer, optionsJson, decodeBase64OrNull(pcmDataBase64)) - if (rc < 0) { - fail(promise, "VAD failed") - return - } - promise.resolve(decodeNullTerminatedUtf8(buffer)) - } - - @ReactMethod - fun diarize(handle: String, audioPath: String?, optionsJson: String?, pcmDataBase64: String?, promise: Promise) { - val nativeHandle = parseHandle(handle, promise) ?: return - val buffer = ByteArray(LARGE_BUFFER_SIZE) - val rc = CactusJNI.nativeDiarize(nativeHandle, audioPath, buffer, optionsJson, decodeBase64OrNull(pcmDataBase64)) - if (rc < 0) { - fail(promise, "Diarize failed") - return - } - promise.resolve(decodeNullTerminatedUtf8(buffer)) - } - - @ReactMethod - fun embedSpeaker( - handle: String, - audioPath: String?, - optionsJson: String?, - pcmDataBase64: String?, - maskWeights: ReadableArray?, - promise: Promise, - ) { - val nativeHandle = parseHandle(handle, promise) ?: return - val buffer = ByteArray(DEFAULT_BUFFER_SIZE) - val mask = maskWeights?.let { readableArrayToFloatArray(it) } - val rc = CactusJNI.nativeEmbedSpeaker( - nativeHandle, - audioPath, - buffer, - optionsJson, - decodeBase64OrNull(pcmDataBase64), - mask, - mask?.size?.toLong() ?: 0L, - ) - if (rc < 0) { - fail(promise, "EmbedSpeaker failed") - return - } - promise.resolve(decodeNullTerminatedUtf8(buffer)) - } - @ReactMethod fun ragQuery(handle: String, query: String, topK: Double, promise: Promise) { val nativeHandle = parseHandle(handle, promise) ?: return diff --git a/bindings/react-native/apple/Cactus.m b/bindings/react-native/apple/Cactus.m index f6f8a61cc..738c7e2af 100644 --- a/bindings/react-native/apple/Cactus.m +++ b/bindings/react-native/apple/Cactus.m @@ -1,22 +1,20 @@ #import +#import -@interface RCT_EXTERN_MODULE(Cactus, NSObject) +@interface RCT_EXTERN_MODULE(Cactus, RCTEventEmitter) RCT_EXTERN_METHOD(init:(NSString *)modelPath corpusDir:(NSString * _Nullable)corpusDir cacheIndex:(BOOL)cacheIndex resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) RCT_EXTERN_METHOD(destroy:(NSString *)handle resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) RCT_EXTERN_METHOD(reset:(NSString *)handle resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) RCT_EXTERN_METHOD(stop:(NSString *)handle resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) RCT_EXTERN_METHOD(prefill:(NSString *)handle messagesJson:(NSString *)messagesJson optionsJson:(NSString * _Nullable)optionsJson toolsJson:(NSString * _Nullable)toolsJson pcmDataBase64:(NSString * _Nullable)pcmDataBase64 resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) -RCT_EXTERN_METHOD(complete:(NSString *)handle messagesJson:(NSString *)messagesJson optionsJson:(NSString * _Nullable)optionsJson toolsJson:(NSString * _Nullable)toolsJson pcmDataBase64:(NSString * _Nullable)pcmDataBase64 resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) +RCT_EXTERN_METHOD(complete:(NSString *)handle messagesJson:(NSString *)messagesJson optionsJson:(NSString * _Nullable)optionsJson toolsJson:(NSString * _Nullable)toolsJson pcmDataBase64:(NSString * _Nullable)pcmDataBase64 streamTokens:(BOOL)streamTokens resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) RCT_EXTERN_METHOD(tokenize:(NSString *)handle text:(NSString *)text resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) RCT_EXTERN_METHOD(scoreWindow:(NSString *)handle tokens:(NSArray *)tokens start:(nonnull NSNumber *)start end:(nonnull NSNumber *)end context:(nonnull NSNumber *)context resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) RCT_EXTERN_METHOD(transcribe:(NSString *)handle audioPath:(NSString * _Nullable)audioPath prompt:(NSString * _Nullable)prompt optionsJson:(NSString * _Nullable)optionsJson pcmDataBase64:(NSString * _Nullable)pcmDataBase64 resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) RCT_EXTERN_METHOD(embed:(NSString *)handle text:(NSString *)text normalize:(BOOL)normalize resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) RCT_EXTERN_METHOD(imageEmbed:(NSString *)handle imagePath:(NSString *)imagePath resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) RCT_EXTERN_METHOD(audioEmbed:(NSString *)handle audioPath:(NSString *)audioPath resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) -RCT_EXTERN_METHOD(vad:(NSString *)handle audioPath:(NSString * _Nullable)audioPath optionsJson:(NSString * _Nullable)optionsJson pcmDataBase64:(NSString * _Nullable)pcmDataBase64 resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) -RCT_EXTERN_METHOD(diarize:(NSString *)handle audioPath:(NSString * _Nullable)audioPath optionsJson:(NSString * _Nullable)optionsJson pcmDataBase64:(NSString * _Nullable)pcmDataBase64 resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) -RCT_EXTERN_METHOD(embedSpeaker:(NSString *)handle audioPath:(NSString * _Nullable)audioPath optionsJson:(NSString * _Nullable)optionsJson pcmDataBase64:(NSString * _Nullable)pcmDataBase64 maskWeights:(NSArray * _Nullable)maskWeights resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) RCT_EXTERN_METHOD(ragQuery:(NSString *)handle query:(NSString *)query topK:(nonnull NSNumber *)topK resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) RCT_EXTERN_METHOD(indexInit:(NSString *)indexDir embeddingDim:(nonnull NSNumber *)embeddingDim resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) RCT_EXTERN_METHOD(indexAdd:(NSString *)handle ids:(NSArray *)ids documents:(NSArray *)documents embeddings:(NSArray *> *)embeddings metadatas:(NSArray * _Nullable)metadatas resolver:(RCTPromiseResolveBlock)resolve rejecter:(RCTPromiseRejectBlock)reject) diff --git a/bindings/react-native/apple/Cactus.swift b/bindings/react-native/apple/Cactus.swift index a9732a35c..bdb61da46 100644 --- a/bindings/react-native/apple/Cactus.swift +++ b/bindings/react-native/apple/Cactus.swift @@ -3,7 +3,7 @@ import React import cactus @objc(Cactus) -final class Cactus: NSObject { +final class Cactus: RCTEventEmitter { private let defaultBufferSize = 65_536 private let largeBufferSize = 1 << 20 private let defaultEmbeddingBufferSize = 4096 @@ -13,10 +13,14 @@ final class Cactus: NSObject { private let defaultIndexEmbeddingBufferSize = 4096 @objc - static func requiresMainQueueSetup() -> Bool { + override static func requiresMainQueueSetup() -> Bool { false } + override func supportedEvents() -> [String] { + ["onToken"] + } + private func lastError(_ fallback: String) -> String { guard let ptr = cactus_get_last_error() else { return fallback } return String(cString: ptr) @@ -109,13 +113,14 @@ final class Cactus: NSObject { resolve(stringFromBuffer(buffer)) } - @objc(complete:messagesJson:optionsJson:toolsJson:pcmDataBase64:resolver:rejecter:) + @objc(complete:messagesJson:optionsJson:toolsJson:pcmDataBase64:streamTokens:resolver:rejecter:) func complete( _ handle: String, messagesJson: String, optionsJson: String?, toolsJson: String?, pcmDataBase64: String?, + streamTokens: Bool, resolver resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock ) { @@ -124,9 +129,28 @@ final class Cactus: NSObject { self.reject(reject, "Invalid native handle") return } + + var cb: cactus_token_callback? = nil + var userData: UnsafeMutableRawPointer? = nil + if streamTokens { + let emitter = Unmanaged.passRetained(self).toOpaque() + userData = emitter + cb = { tokenPtr, tokenId, ctx in + guard let ctx = ctx, let tokenPtr = tokenPtr else { return } + let emitter = Unmanaged.fromOpaque(ctx).takeUnretainedValue() + let token = String(cString: tokenPtr) + emitter.sendEvent(withName: "onToken", body: ["token": token, "tokenId": tokenId]) + } + } + let rc = decodeBase64(pcmDataBase64)?.withUnsafeBytes { rawBuffer in - cactus_complete(nativeHandle, messagesJson, &buffer, buffer.count, optionsJson, toolsJson, nil, nil, rawBuffer.bindMemory(to: UInt8.self).baseAddress, rawBuffer.count) - } ?? cactus_complete(nativeHandle, messagesJson, &buffer, buffer.count, optionsJson, toolsJson, nil, nil, nil, 0) + cactus_complete(nativeHandle, messagesJson, &buffer, buffer.count, optionsJson, toolsJson, cb, userData, rawBuffer.bindMemory(to: UInt8.self).baseAddress, rawBuffer.count) + } ?? cactus_complete(nativeHandle, messagesJson, &buffer, buffer.count, optionsJson, toolsJson, cb, userData, nil, 0) + + if streamTokens, let userData = userData { + Unmanaged.fromOpaque(userData).release() + } + guard rc >= 0 else { self.reject(reject, "Completion failed") return @@ -248,88 +272,6 @@ final class Cactus: NSObject { resolve(buffer.prefix(dim).map(Double.init)) } - @objc(vad:audioPath:optionsJson:pcmDataBase64:resolver:rejecter:) - func vad( - _ handle: String, - audioPath: String?, - optionsJson: String?, - pcmDataBase64: String?, - resolver resolve: RCTPromiseResolveBlock, - rejecter reject: RCTPromiseRejectBlock - ) { - guard let nativeHandle = decodeHandle(handle) else { - self.reject(reject, "Invalid native handle") - return - } - var buffer = [CChar](repeating: 0, count: defaultBufferSize) - let rc = decodeBase64(pcmDataBase64)?.withUnsafeBytes { rawBuffer in - cactus_vad(nativeHandle, audioPath, &buffer, buffer.count, optionsJson, rawBuffer.bindMemory(to: UInt8.self).baseAddress, rawBuffer.count) - } ?? cactus_vad(nativeHandle, audioPath, &buffer, buffer.count, optionsJson, nil, 0) - guard rc >= 0 else { - self.reject(reject, "VAD failed") - return - } - resolve(stringFromBuffer(buffer)) - } - - @objc(diarize:audioPath:optionsJson:pcmDataBase64:resolver:rejecter:) - func diarize( - _ handle: String, - audioPath: String?, - optionsJson: String?, - pcmDataBase64: String?, - resolver resolve: RCTPromiseResolveBlock, - rejecter reject: RCTPromiseRejectBlock - ) { - guard let nativeHandle = decodeHandle(handle) else { - self.reject(reject, "Invalid native handle") - return - } - var buffer = [CChar](repeating: 0, count: largeBufferSize) - let rc = decodeBase64(pcmDataBase64)?.withUnsafeBytes { rawBuffer in - cactus_diarize(nativeHandle, audioPath, &buffer, buffer.count, optionsJson, rawBuffer.bindMemory(to: UInt8.self).baseAddress, rawBuffer.count) - } ?? cactus_diarize(nativeHandle, audioPath, &buffer, buffer.count, optionsJson, nil, 0) - guard rc >= 0 else { - self.reject(reject, "Diarize failed") - return - } - resolve(stringFromBuffer(buffer)) - } - - @objc(embedSpeaker:audioPath:optionsJson:pcmDataBase64:maskWeights:resolver:rejecter:) - func embedSpeaker( - _ handle: String, - audioPath: String?, - optionsJson: String?, - pcmDataBase64: String?, - maskWeights: [NSNumber]?, - resolver resolve: RCTPromiseResolveBlock, - rejecter reject: RCTPromiseRejectBlock - ) { - guard let nativeHandle = decodeHandle(handle) else { - self.reject(reject, "Invalid native handle") - return - } - var buffer = [CChar](repeating: 0, count: defaultBufferSize) - var mask = maskWeights?.map { Float(truncating: $0) } - let rc = decodeBase64(pcmDataBase64)?.withUnsafeBytes { rawBuffer in - if var mask { - return cactus_embed_speaker(nativeHandle, audioPath, &buffer, buffer.count, optionsJson, rawBuffer.bindMemory(to: UInt8.self).baseAddress, rawBuffer.count, &mask, mask.count) - } - return cactus_embed_speaker(nativeHandle, audioPath, &buffer, buffer.count, optionsJson, rawBuffer.bindMemory(to: UInt8.self).baseAddress, rawBuffer.count, nil, 0) - } ?? { - if var mask { - return cactus_embed_speaker(nativeHandle, audioPath, &buffer, buffer.count, optionsJson, nil, 0, &mask, mask.count) - } - return cactus_embed_speaker(nativeHandle, audioPath, &buffer, buffer.count, optionsJson, nil, 0, nil, 0) - }() - guard rc >= 0 else { - self.reject(reject, "EmbedSpeaker failed") - return - } - resolve(stringFromBuffer(buffer)) - } - @objc(ragQuery:query:topK:resolver:rejecter:) func ragQuery(_ handle: String, query: String, topK: NSNumber, resolver resolve: RCTPromiseResolveBlock, rejecter reject: RCTPromiseRejectBlock) { guard let nativeHandle = decodeHandle(handle) else { diff --git a/bindings/react-native/index.ts b/bindings/react-native/index.ts index 96b3a0e7c..ad93cdf97 100644 --- a/bindings/react-native/index.ts +++ b/bindings/react-native/index.ts @@ -1,4 +1,4 @@ -import {NativeModules} from 'react-native'; +import {NativeModules, NativeEventEmitter} from 'react-native'; export interface CactusNativeModule { init(modelPath: string, corpusDir: string | null, cacheIndex: boolean): Promise; @@ -18,6 +18,7 @@ export interface CactusNativeModule { optionsJson: string | null, toolsJson: string | null, pcmDataBase64: string | null, + streamTokens: boolean, ): Promise; tokenize(handle: string, text: string): Promise; scoreWindow( @@ -37,25 +38,6 @@ export interface CactusNativeModule { embed(handle: string, text: string, normalize: boolean): Promise; imageEmbed(handle: string, imagePath: string): Promise; audioEmbed(handle: string, audioPath: string): Promise; - vad( - handle: string, - audioPath: string | null, - optionsJson: string | null, - pcmDataBase64: string | null, - ): Promise; - diarize( - handle: string, - audioPath: string | null, - optionsJson: string | null, - pcmDataBase64: string | null, - ): Promise; - embedSpeaker( - handle: string, - audioPath: string | null, - optionsJson: string | null, - pcmDataBase64: string | null, - maskWeights: number[] | null, - ): Promise; ragQuery(handle: string, query: string, topK: number): Promise; indexInit(indexDir: string, embeddingDim: number): Promise; indexAdd( @@ -94,4 +76,8 @@ if (!nativeModule) { export const Cactus = nativeModule; +export const CactusEvents = new NativeEventEmitter(NativeModules.Cactus); + +export type TokenEvent = {token: string; tokenId: number}; + export default Cactus; diff --git a/bindings/rust/cactus.rs b/bindings/rust/cactus.rs index 5b10d7c57..e05ddeb01 100644 --- a/bindings/rust/cactus.rs +++ b/bindings/rust/cactus.rs @@ -21,9 +21,6 @@ extern "C" { pub fn cactus_image_embed(model: CactusModelT, image_path: *const c_char, embeddings_buffer: *mut c_float, buffer_size: usize, embedding_dim: *mut usize) -> c_int; pub fn cactus_audio_embed(model: CactusModelT, audio_path: *const c_char, embeddings_buffer: *mut c_float, buffer_size: usize, embedding_dim: *mut usize) -> c_int; - pub fn cactus_vad(model: CactusModelT, audio_file_path: *const c_char, response_buffer: *mut c_char, buffer_size: usize, options_json: *const c_char, pcm_buffer: *const u8, pcm_buffer_size: usize) -> c_int; - pub fn cactus_diarize(model: CactusModelT, audio_file_path: *const c_char, response_buffer: *mut c_char, buffer_size: usize, options_json: *const c_char, pcm_buffer: *const u8, pcm_buffer_size: usize) -> c_int; - pub fn cactus_embed_speaker(model: CactusModelT, audio_file_path: *const c_char, response_buffer: *mut c_char, buffer_size: usize, options_json: *const c_char, pcm_buffer: *const u8, pcm_buffer_size: usize, mask_weights: *const c_float, mask_num_frames: usize) -> c_int; pub fn cactus_rag_query(model: CactusModelT, query: *const c_char, response_buffer: *mut c_char, buffer_size: usize, top_k: usize) -> c_int; pub fn cactus_index_init(index_dir: *const c_char, embedding_dim: usize) -> CactusIndexT; diff --git a/cactus-engine/CMakeLists.txt b/cactus-engine/CMakeLists.txt index b683acf66..d8fe682b3 100644 --- a/cactus-engine/CMakeLists.txt +++ b/cactus-engine/CMakeLists.txt @@ -2,36 +2,10 @@ cmake_minimum_required(VERSION 3.10) project(CactusEngine LANGUAGES CXX) set(CMAKE_CXX_STANDARD 20) -set(CMAKE_CXX_STANDARD_REQUIRED True) +set(CMAKE_CXX_STANDARD_REQUIRED ON) -if(NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE Release) -endif() - -if(APPLE) - set(CMAKE_OSX_ARCHITECTURES "arm64") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -arch arm64 -march=armv8.2-a+fp16+simd+dotprod+i8mm -pthread -Wall -Wextra -pedantic -O3 -Wno-missing-field-initializers") - add_compile_definitions( - __ARM_NEON=1 - __ARM_FEATURE_FP16_VECTOR_ARITHMETIC=1 - __ARM_FEATURE_DOTPROD=1 - __ARM_FEATURE_MATMUL_INT8=1 - ACCELERATE_NEW_LAPACK - ) -else() - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=armv8.2-a+fp16+simd+dotprod+i8mm -pthread -Wall -Wextra -pedantic -O3 -Wno-missing-field-initializers") - add_compile_definitions( - __ARM_NEON=1 - __ARM_FEATURE_FP16_VECTOR_ARITHMETIC=1 - __ARM_FEATURE_DOTPROD=1 - __ARM_FEATURE_MATMUL_INT8=1 - ) -endif() - -# --- Depend on cactus-graph (which depends on cactus-kernels) --- -set(CACTUS_GRAPH_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../cactus-graph") if(NOT TARGET cactus_graph) - add_subdirectory(${CACTUS_GRAPH_DIR} ${CMAKE_CURRENT_BINARY_DIR}/cactus-graph) + add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../cactus-graph ${CMAKE_CURRENT_BINARY_DIR}/cactus-graph) endif() # --- libcurl --- @@ -68,7 +42,7 @@ else() set(CACTUS_HAS_CURL ${CURL_FOUND}) endif() -# --- Engine sources --- +# --- Sources --- set(ENGINE_SOURCES src/bpe.cpp src/sp.cpp @@ -98,30 +72,34 @@ else() endif() add_library(cactus_engine STATIC ${ENGINE_SOURCES}) -set(CACTUS_KERNELS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../cactus-kernels") + +set_target_properties(cactus_engine PROPERTIES + POSITION_INDEPENDENT_CODE ON +) + target_include_directories(cactus_engine PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src ${CMAKE_CURRENT_SOURCE_DIR}/libs - ${CACTUS_KERNELS_DIR}/src - ${CACTUS_KERNELS_DIR}/libs + ${CMAKE_CURRENT_SOURCE_DIR}/../cactus-kernels/src + ${CMAKE_CURRENT_SOURCE_DIR}/../cactus-kernels/libs ) + target_link_libraries(cactus_engine PUBLIC cactus_graph) -set_target_properties(cactus_engine PROPERTIES POSITION_INDEPENDENT_CODE ON) -# curl +# --- curl linking --- if(CACTUS_HAS_CURL) if(APPLE OR ANDROID) - target_link_libraries(cactus_engine PUBLIC "${CACTUS_CURL_LIBRARY}") - target_include_directories(cactus_engine PUBLIC "${CACTUS_CURL_INCLUDE_DIR}") + target_link_libraries(cactus_engine PRIVATE "${CACTUS_CURL_LIBRARY}") + target_include_directories(cactus_engine PRIVATE "${CACTUS_CURL_INCLUDE_DIR}") else() - target_link_libraries(cactus_engine PUBLIC ${CURL_LIBRARIES}) + target_link_libraries(cactus_engine PRIVATE ${CURL_LIBRARIES}) target_include_directories(cactus_engine PRIVATE ${CURL_INCLUDE_DIRS}) endif() target_compile_definitions(cactus_engine PRIVATE CACTUS_USE_CURL=1) endif() -# Apple frameworks +# --- Apple frameworks --- if(APPLE) find_library(COREML_FRAMEWORK CoreML REQUIRED) find_library(FOUNDATION_FRAMEWORK Foundation REQUIRED) @@ -129,15 +107,12 @@ if(APPLE) find_library(SYSTEMCONFIGURATION_FRAMEWORK SystemConfiguration REQUIRED) find_library(CFNETWORK_FRAMEWORK CFNetwork REQUIRED) target_link_libraries(cactus_engine PUBLIC - ${COREML_FRAMEWORK} - ${FOUNDATION_FRAMEWORK} - ${SECURITY_FRAMEWORK} - ${SYSTEMCONFIGURATION_FRAMEWORK} - ${CFNETWORK_FRAMEWORK} + ${COREML_FRAMEWORK} ${FOUNDATION_FRAMEWORK} + ${SECURITY_FRAMEWORK} ${SYSTEMCONFIGURATION_FRAMEWORK} ${CFNETWORK_FRAMEWORK} ) endif() -# Version +# --- Version --- set(_CACTUS_VERSION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/../CACTUS_VERSION") if(EXISTS "${_CACTUS_VERSION_FILE}") file(READ "${_CACTUS_VERSION_FILE}" _CACTUS_VERSION_CONTENT) @@ -145,8 +120,8 @@ if(EXISTS "${_CACTUS_VERSION_FILE}") target_compile_definitions(cactus_engine PRIVATE CACTUS_COMPILE_TIME_VERSION="${CACTUS_VERSION_STR}") endif() -if(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang") - target_compile_options(cactus_engine PRIVATE -Wno-c99-extensions) -elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") - target_compile_options(cactus_engine PRIVATE -Wno-pedantic) -endif() +target_compile_options(cactus_engine PRIVATE + -Wall -Wextra -pedantic -Wno-missing-field-initializers + $<$:-Wno-c99-extensions> + $<$:-Wno-pedantic> +) diff --git a/cactus-engine/cactus_engine.h b/cactus-engine/cactus_engine.h index ce65fbf08..dbb9d5138 100644 --- a/cactus-engine/cactus_engine.h +++ b/cactus-engine/cactus_engine.h @@ -112,38 +112,6 @@ CACTUS_FFI_EXPORT int cactus_audio_embed( size_t* embedding_dim ); -CACTUS_FFI_EXPORT int cactus_vad( - cactus_model_t model, - const char* audio_file_path, - char* response_buffer, - size_t buffer_size, - const char* options_json, - const uint8_t* pcm_buffer, - size_t pcm_buffer_size -); - -CACTUS_FFI_EXPORT int cactus_diarize( - cactus_model_t model, - const char* audio_file_path, - char* response_buffer, - size_t buffer_size, - const char* options_json, - const uint8_t* pcm_buffer, - size_t pcm_buffer_size -); - -CACTUS_FFI_EXPORT int cactus_embed_speaker( - cactus_model_t model, - const char* audio_file_path, - char* response_buffer, - size_t buffer_size, - const char* options_json, - const uint8_t* pcm_buffer, - size_t pcm_buffer_size, - const float* mask_weights, - size_t mask_num_frames -); - CACTUS_FFI_EXPORT int cactus_rag_query( cactus_model_t model, const char* query, diff --git a/cactus-engine/src/engine.h b/cactus-engine/src/engine.h index a06791308..0899634b2 100644 --- a/cactus-engine/src/engine.h +++ b/cactus-engine/src/engine.h @@ -170,7 +170,7 @@ struct Config { float rope_scaling_factor = 1.0f; float rope_mscale_all_dim = 0.0f; - enum class ModelType {QWEN = 0, GEMMA = 1, NOMIC = 3, LFM2 = 5, SIGLIP2 = 6, WHISPER = 7, MOONSHINE = 8, SILERO_VAD = 9, PARAKEET = 10, QWEN3P5 = 11, PARAKEET_TDT = 12, GEMMA3N = 13, YOUTU = 14, GEMMA4 = 15, PYANNOTE = 16, WESPEAKER = 17, NEEDLE = 18}; + enum class ModelType {QWEN = 0, GEMMA = 1, NOMIC = 3, LFM2 = 5, SIGLIP2 = 6, WHISPER = 7, MOONSHINE = 8, PARAKEET = 10, QWEN3P5 = 11, PARAKEET_TDT = 12, GEMMA3N = 13, YOUTU = 14, GEMMA4 = 15, NEEDLE = 18}; uint32_t predictor_hidden_dim = 0; uint32_t predictor_num_layers = 0; uint32_t tdt_joint_dim = 0; diff --git a/cactus-engine/src/model.cpp b/cactus-engine/src/model.cpp index e4d56ebb4..fec6f94f5 100644 --- a/cactus-engine/src/model.cpp +++ b/cactus-engine/src/model.cpp @@ -148,7 +148,7 @@ bool Model::init(const std::string& bundle_dir, size_t context_size, encoder_ = components_.count("lm_encoder_step") ? &components_.at("lm_encoder_step") : nullptr; decoder_ = components_.count("decoder_step") ? &components_.at("decoder_step") : nullptr; - const bool is_lm = (encoder_ != nullptr && decoder_ != nullptr); + const bool is_lm = (decoder_ != nullptr); const bool is_whisper_transcription = config_.model_type == Config::ModelType::WHISPER && components_.count("audio_encoder") && @@ -370,10 +370,15 @@ void Model::copy_encoder_outputs_to_decoder(const Component& enc) { } void Model::run_step(uint32_t token_id, size_t position, bool /*read_logits*/) { - write_int_input(*encoder_, "input_ids", static_cast(token_id)); - write_int_input(*encoder_, "position_ids", static_cast(position)); - encoder_->graph->execute(); - copy_encoder_outputs_to_decoder(*encoder_); + if (encoder_) { + write_int_input(*encoder_, "input_ids", static_cast(token_id)); + write_int_input(*encoder_, "position_ids", static_cast(position)); + encoder_->graph->execute(); + copy_encoder_outputs_to_decoder(*encoder_); + } else { + write_int_input(*decoder_, "input_ids", static_cast(token_id)); + write_int_input(*decoder_, "position_ids", static_cast(position)); + } decoder_->graph->execute(); } diff --git a/cactus-engine/src/utils.h b/cactus-engine/src/utils.h index c09b7d688..1e18fa899 100644 --- a/cactus-engine/src/utils.h +++ b/cactus-engine/src/utils.h @@ -173,27 +173,6 @@ inline cactus::engine::SpectrogramConfig get_gemma4_audio_spectrogram_config( return cfg; } -inline cactus::engine::SpectrogramConfig get_wespeaker_spectrogram_config() { - cactus::engine::SpectrogramConfig cfg{}; - cfg.n_fft = 512; - cfg.frame_length = 400; - cfg.hop_length = 160; - cfg.power = 2.0f; - cfg.center = false; - cfg.pad_mode = "constant"; - cfg.onesided = true; - cfg.dither = 0.0f; - cfg.mel_floor = 1.1754944e-38f; - cfg.log_mel = "log"; - cfg.reference = 1.0f; - cfg.min_value = 1.1754944e-38f; - cfg.remove_dc_offset = true; - cfg.preemphasis = 0.97f; - cfg.hann_periodic = false; - cfg.window_a0 = 0.54f; - return cfg; -} - // Whisper v1/v2: 80 mel bins, HTK. Whisper v3: 128 mel bins, Slaney, 512-FFT, no DC removal. inline void apply_whisper_v3_overrides(cactus::engine::SpectrogramConfig& cfg) { cfg.fft_override = 512; diff --git a/cactus-engine/tests/CMakeLists.txt b/cactus-engine/tests/CMakeLists.txt index 30716f477..ebd22f639 100644 --- a/cactus-engine/tests/CMakeLists.txt +++ b/cactus-engine/tests/CMakeLists.txt @@ -7,7 +7,6 @@ set(CMAKE_CXX_STANDARD_REQUIRED True) set(CACTUS_ENGINE_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/..) set(CACTUS_PROJECT_ROOT ${CMAKE_CURRENT_SOURCE_DIR}/../..) -# --- libcurl --- if(APPLE) set(CMAKE_OSX_ARCHITECTURES "arm64") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -arch arm64 -pthread -Wall -Wextra -pedantic -O3") @@ -22,13 +21,11 @@ else() find_package(CURL) endif() -# --- Find the pre-built cactus library --- set(CACTUS_LIB "${CACTUS_PROJECT_ROOT}/cactus/build/libcactus.a") if(NOT EXISTS "${CACTUS_LIB}") message(FATAL_ERROR "Missing ${CACTUS_LIB}; build the top-level cactus target first") endif() -# --- Platform libraries --- if(NOT APPLE) find_library(MMAN_LIB mman) else() @@ -40,45 +37,41 @@ else() find_library(CFNETWORK_FRAMEWORK CFNetwork REQUIRED) endif() -# --- Test executables --- -file(GLOB TEST_SOURCES CONFIGURE_DEPENDS "test_*.cpp") -list(REMOVE_ITEM TEST_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/test_utils.cpp") - -foreach(TEST_FILE ${TEST_SOURCES}) - get_filename_component(TEST_NAME ${TEST_FILE} NAME_WE) - add_executable(${TEST_NAME} ${TEST_FILE} test_utils.cpp) - target_link_libraries(${TEST_NAME} PRIVATE ${CACTUS_LIB}) - - target_include_directories(${TEST_NAME} PRIVATE +function(cactus_link_target target_name) + target_link_libraries(${target_name} PRIVATE ${CACTUS_LIB}) + target_include_directories(${target_name} PRIVATE ${CACTUS_ENGINE_ROOT} ${CACTUS_PROJECT_ROOT}/cactus-graph ${CACTUS_PROJECT_ROOT}/cactus-kernels + ${CACTUS_PROJECT_ROOT}/cactus-kernels/src ) - if(MMAN_LIB) - target_link_libraries(${TEST_NAME} PRIVATE ${MMAN_LIB}) + target_link_libraries(${target_name} PRIVATE ${MMAN_LIB}) endif() - if(APPLE) - target_link_libraries(${TEST_NAME} PRIVATE - ${ACCELERATE_FRAMEWORK} - ${COREML_FRAMEWORK} - ${FOUNDATION_FRAMEWORK} - ${SECURITY_FRAMEWORK} - ${SYSTEMCONFIGURATION_FRAMEWORK} - ${CFNETWORK_FRAMEWORK} + target_link_libraries(${target_name} PRIVATE + ${ACCELERATE_FRAMEWORK} ${COREML_FRAMEWORK} ${FOUNDATION_FRAMEWORK} + ${SECURITY_FRAMEWORK} ${SYSTEMCONFIGURATION_FRAMEWORK} ${CFNETWORK_FRAMEWORK} ) if(CACTUS_CURL_LIBRARY_MACOS) - target_link_libraries(${TEST_NAME} PRIVATE ${CACTUS_CURL_LIBRARY_MACOS}) - target_include_directories(${TEST_NAME} PRIVATE ${CACTUS_CURL_INCLUDE_DIR}) + target_link_libraries(${target_name} PRIVATE ${CACTUS_CURL_LIBRARY_MACOS}) + target_include_directories(${target_name} PRIVATE ${CACTUS_CURL_INCLUDE_DIR}) endif() elseif(CURL_FOUND) - target_link_libraries(${TEST_NAME} PRIVATE ${CURL_LIBRARIES}) - target_include_directories(${TEST_NAME} PRIVATE ${CURL_INCLUDE_DIRS}) + target_link_libraries(${target_name} PRIVATE ${CURL_LIBRARIES}) + target_include_directories(${target_name} PRIVATE ${CURL_INCLUDE_DIRS}) endif() +endfunction() + +file(GLOB TEST_SOURCES CONFIGURE_DEPENDS "test_*.cpp") +list(REMOVE_ITEM TEST_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/test_utils.cpp") + +foreach(TEST_FILE ${TEST_SOURCES}) + get_filename_component(TEST_NAME ${TEST_FILE} NAME_WE) + add_executable(${TEST_NAME} ${TEST_FILE} test_utils.cpp) + cactus_link_target(${TEST_NAME}) endforeach() -# --- chat CLI (depends on SDL2 for audio recording) --- find_package(SDL2 QUIET CONFIG) if(NOT SDL2_FOUND) find_library(SDL2_LIBRARY NAMES SDL2 PATHS /opt/homebrew/lib /usr/local/lib) @@ -89,67 +82,14 @@ if(NOT SDL2_FOUND) endif() if(SDL2_FOUND) add_executable(chat chat.cpp test_utils.cpp) - target_link_libraries(chat PRIVATE ${CACTUS_LIB}) + cactus_link_target(chat) if(TARGET SDL2::SDL2) target_link_libraries(chat PRIVATE SDL2::SDL2) else() target_link_libraries(chat PRIVATE ${SDL2_LIBRARY}) target_include_directories(chat PRIVATE ${SDL2_INCLUDE_DIR}) endif() - target_include_directories(chat PRIVATE - ${CACTUS_ENGINE_ROOT} - ${CACTUS_PROJECT_ROOT}/cactus-graph - ${CACTUS_PROJECT_ROOT}/cactus-kernels - ) - if(MMAN_LIB) - target_link_libraries(chat PRIVATE ${MMAN_LIB}) - endif() - if(APPLE) - target_link_libraries(chat PRIVATE - ${ACCELERATE_FRAMEWORK} - ${COREML_FRAMEWORK} - ${FOUNDATION_FRAMEWORK} - ${SECURITY_FRAMEWORK} - ${SYSTEMCONFIGURATION_FRAMEWORK} - ${CFNETWORK_FRAMEWORK} - ) - if(CACTUS_CURL_LIBRARY_MACOS) - target_link_libraries(chat PRIVATE ${CACTUS_CURL_LIBRARY_MACOS}) - target_include_directories(chat PRIVATE ${CACTUS_CURL_INCLUDE_DIR}) - endif() - elseif(CURL_FOUND) - target_link_libraries(chat PRIVATE ${CURL_LIBRARIES}) - target_include_directories(chat PRIVATE ${CURL_INCLUDE_DIRS}) - endif() -else() - message(WARNING "SDL2 not found; chat binary will not be built") endif() -# --- asr CLI (audio transcription via cactus_transcribe) --- add_executable(asr asr.cpp) -target_link_libraries(asr PRIVATE ${CACTUS_LIB}) -target_include_directories(asr PRIVATE - ${CACTUS_ENGINE_ROOT} - ${CACTUS_PROJECT_ROOT}/cactus-graph - ${CACTUS_PROJECT_ROOT}/cactus-kernels -) -if(MMAN_LIB) - target_link_libraries(asr PRIVATE ${MMAN_LIB}) -endif() -if(APPLE) - target_link_libraries(asr PRIVATE - ${ACCELERATE_FRAMEWORK} - ${COREML_FRAMEWORK} - ${FOUNDATION_FRAMEWORK} - ${SECURITY_FRAMEWORK} - ${SYSTEMCONFIGURATION_FRAMEWORK} - ${CFNETWORK_FRAMEWORK} - ) - if(CACTUS_CURL_LIBRARY_MACOS) - target_link_libraries(asr PRIVATE ${CACTUS_CURL_LIBRARY_MACOS}) - target_include_directories(asr PRIVATE ${CACTUS_CURL_INCLUDE_DIR}) - endif() -elseif(CURL_FOUND) - target_link_libraries(asr PRIVATE ${CURL_LIBRARIES}) - target_include_directories(asr PRIVATE ${CURL_INCLUDE_DIRS}) -endif() +cactus_link_target(asr) diff --git a/cactus-engine/tests/android/CMakeLists.txt b/cactus-engine/tests/android/CMakeLists.txt index 02fa18c87..a1764f8d8 100644 --- a/cactus-engine/tests/android/CMakeLists.txt +++ b/cactus-engine/tests/android/CMakeLists.txt @@ -11,7 +11,7 @@ set(CACTUS_GRAPH_DIR ${PROJECT_ROOT}/cactus-graph) set(CACTUS_KERNELS_DIR ${PROJECT_ROOT}/cactus-kernels) set(ANDROID_BUILD_DIR ${PROJECT_ROOT}/android/build) -set(CACTUS_LIB ${ANDROID_BUILD_DIR}/lib/libcactus_static.a) +set(CACTUS_LIB ${ANDROID_BUILD_DIR}/cactus/libcactus.a) set(CACTUS_CURL_ROOT "$ENV{CACTUS_CURL_ROOT}") if(NOT CACTUS_CURL_ROOT) @@ -76,6 +76,7 @@ foreach(TEST_FILE ${TEST_SOURCES}) ${CACTUS_ENGINE_DIR} ${CACTUS_GRAPH_DIR} ${CACTUS_KERNELS_DIR} + ${CACTUS_KERNELS_DIR}/src ) message(STATUS "Added Android test: ${TEST_NAME}") endforeach() diff --git a/cactus-engine/tests/android/run.sh b/cactus-engine/tests/android/run.sh index 33a0aff50..db94a4cb2 100755 --- a/cactus-engine/tests/android/run.sh +++ b/cactus-engine/tests/android/run.sh @@ -212,16 +212,16 @@ cmake -DCMAKE_TOOLCHAIN_FILE="$cmake_toolchain" \ -S "$PROJECT_ROOT/android" \ -B "$android_lib_build_dir" >/dev/null -if ! cmake --build "$android_lib_build_dir" --target cactus_static -j "$n_jobs" >/dev/null; then +if ! cmake --build "$android_lib_build_dir" --target cactus -j "$n_jobs" >/dev/null; then echo "Failed to build Cactus static library" exit 1 fi -if [ ! -f "$android_lib_build_dir/lib/libcactus_static.a" ]; then - echo "Static library not found at $android_lib_build_dir/lib/libcactus_static.a" +if [ ! -f "$android_lib_build_dir/cactus/libcactus.a" ]; then + echo "Static library not found at $android_lib_build_dir/cactus/libcactus.a" exit 1 fi -echo "Static library built: $android_lib_build_dir/lib/libcactus_static.a" +echo "Static library built: $android_lib_build_dir/cactus/libcactus.a" echo "" echo "Step 3: Building Android tests..." diff --git a/cactus-engine/tests/ios/configure_xcode.rb b/cactus-engine/tests/ios/configure_xcode.rb index 051503849..0490e8e47 100755 --- a/cactus-engine/tests/ios/configure_xcode.rb +++ b/cactus-engine/tests/ios/configure_xcode.rb @@ -203,7 +203,7 @@ def generate_app_delegate(output_path, test_files) target.build_configurations.each do |config| config.build_settings['HEADER_SEARCH_PATHS'] ||= ['$(inherited)'] - [tests_root, cactus_dir, cactus_engine_dir, cactus_graph_dir, cactus_kernels_dir].each do |path| + [tests_root, cactus_dir, cactus_engine_dir, cactus_graph_dir, cactus_kernels_dir, File.join(cactus_kernels_dir, 'src')].each do |path| config.build_settings['HEADER_SEARCH_PATHS'] << path unless config.build_settings['HEADER_SEARCH_PATHS'].include?(path) end if curl_root && !curl_root.empty? diff --git a/cactus-graph/CMakeLists.txt b/cactus-graph/CMakeLists.txt index 975d3c73a..e99787137 100644 --- a/cactus-graph/CMakeLists.txt +++ b/cactus-graph/CMakeLists.txt @@ -2,38 +2,13 @@ cmake_minimum_required(VERSION 3.10) project(CactusGraph LANGUAGES CXX) set(CMAKE_CXX_STANDARD 20) -set(CMAKE_CXX_STANDARD_REQUIRED True) +set(CMAKE_CXX_STANDARD_REQUIRED ON) -if(NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE Release) -endif() - -if(APPLE) - set(CMAKE_OSX_ARCHITECTURES "arm64") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -arch arm64 -march=armv8.2-a+fp16+simd+dotprod+i8mm -pthread -Wall -Wextra -pedantic -O3 -Wno-missing-field-initializers") - add_compile_definitions( - __ARM_NEON=1 - __ARM_FEATURE_FP16_VECTOR_ARITHMETIC=1 - __ARM_FEATURE_DOTPROD=1 - __ARM_FEATURE_MATMUL_INT8=1 - ACCELERATE_NEW_LAPACK - ) -else() - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=armv8.2-a+fp16+simd+dotprod+i8mm -pthread -Wall -Wextra -pedantic -O3 -Wno-missing-field-initializers") - add_compile_definitions( - __ARM_NEON=1 - __ARM_FEATURE_FP16_VECTOR_ARITHMETIC=1 - __ARM_FEATURE_DOTPROD=1 - __ARM_FEATURE_MATMUL_INT8=1 - ) -endif() - -set(CACTUS_KERNELS_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../cactus-kernels") if(NOT TARGET cactus_kernels) - add_subdirectory(${CACTUS_KERNELS_DIR} ${CMAKE_CURRENT_BINARY_DIR}/cactus-kernels) + add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../cactus-kernels ${CMAKE_CURRENT_BINARY_DIR}/cactus-kernels) endif() -set(GRAPH_SOURCES +add_library(cactus_graph STATIC src/core.cpp src/builder.cpp src/execute.cpp @@ -50,33 +25,26 @@ set(GRAPH_SOURCES src/ops_image.cpp ) -add_library(cactus_graph STATIC ${GRAPH_SOURCES}) +set_target_properties(cactus_graph PROPERTIES POSITION_INDEPENDENT_CODE ON) + target_include_directories(cactus_graph PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/src ) + target_link_libraries(cactus_graph PUBLIC cactus_kernels) -set_target_properties(cactus_graph PROPERTIES POSITION_INDEPENDENT_CODE ON) -if(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang") - target_compile_options(cactus_graph PRIVATE -Wno-c99-extensions) -elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") - target_compile_options(cactus_graph PRIVATE -Wno-pedantic) -endif() +target_compile_options(cactus_graph PRIVATE + -Wall -Wextra -pedantic -Wno-missing-field-initializers + $<$:-Wno-c99-extensions> + $<$:-Wno-pedantic> +) if(BUILD_TESTING OR CACTUS_BUILD_TESTS) - if(APPLE) - find_library(ACCELERATE_FRAMEWORK Accelerate REQUIRED) - endif() - file(GLOB TEST_SOURCES "tests/test_*.cpp") foreach(TEST_SRC ${TEST_SOURCES}) get_filename_component(TEST_NAME ${TEST_SRC} NAME_WE) add_executable(${TEST_NAME} ${TEST_SRC}) target_link_libraries(${TEST_NAME} PRIVATE cactus_graph) - target_include_directories(${TEST_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) - if(APPLE) - target_link_libraries(${TEST_NAME} PRIVATE ${ACCELERATE_FRAMEWORK}) - endif() endforeach() endif() diff --git a/cactus-kernels/CMakeLists.txt b/cactus-kernels/CMakeLists.txt index 54961ccff..5312512da 100644 --- a/cactus-kernels/CMakeLists.txt +++ b/cactus-kernels/CMakeLists.txt @@ -2,74 +2,59 @@ cmake_minimum_required(VERSION 3.10) project(CactusKernels LANGUAGES CXX) set(CMAKE_CXX_STANDARD 20) -set(CMAKE_CXX_STANDARD_REQUIRED True) +set(CMAKE_CXX_STANDARD_REQUIRED ON) -if(NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE Release) -endif() - -if(APPLE) - set(CMAKE_OSX_ARCHITECTURES "arm64") - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -arch arm64 -march=armv8.2-a+fp16+simd+dotprod+i8mm -pthread -Wall -Wextra -pedantic -O3 -Wno-missing-field-initializers") - add_compile_definitions( - __ARM_NEON=1 - __ARM_FEATURE_FP16_VECTOR_ARITHMETIC=1 - __ARM_FEATURE_DOTPROD=1 - __ARM_FEATURE_MATMUL_INT8=1 - ACCELERATE_NEW_LAPACK - ) -else() - set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -march=armv8.2-a+fp16+simd+dotprod+i8mm -pthread -Wall -Wextra -pedantic -O3 -Wno-missing-field-initializers") - add_compile_definitions( - __ARM_NEON=1 - __ARM_FEATURE_FP16_VECTOR_ARITHMETIC=1 - __ARM_FEATURE_DOTPROD=1 - __ARM_FEATURE_MATMUL_INT8=1 - ) -endif() - -set(KERNEL_SOURCES +add_library(cactus_kernels STATIC src/attention.cpp + src/attention_hybrid.cpp src/blas.cpp src/conv.cpp src/conv2d.cpp - src/attention_hybrid.cpp - src/norms_rope.cpp src/dsp.cpp src/fused.cpp src/image.cpp src/lstm.cpp src/matmul.cpp src/nn.cpp + src/norms_rope.cpp src/quants.cpp src/reduce.cpp src/scalar.cpp ) -add_library(cactus_kernels STATIC ${KERNEL_SOURCES}) +set_target_properties(cactus_kernels PROPERTIES POSITION_INDEPENDENT_CODE ON) + target_include_directories(cactus_kernels PUBLIC ${CMAKE_CURRENT_SOURCE_DIR} + ${CMAKE_CURRENT_SOURCE_DIR}/src PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/libs ) -set_target_properties(cactus_kernels PROPERTIES POSITION_INDEPENDENT_CODE ON) + +target_compile_options(cactus_kernels PUBLIC + -march=armv8.2-a+fp16+simd+dotprod+i8mm -pthread -O3 +) + +target_compile_definitions(cactus_kernels PUBLIC + __ARM_NEON=1 + __ARM_FEATURE_FP16_VECTOR_ARITHMETIC=1 + __ARM_FEATURE_DOTPROD=1 + __ARM_FEATURE_MATMUL_INT8=1 +) + +target_compile_options(cactus_kernels PRIVATE + -Wall -Wextra -pedantic -Wno-missing-field-initializers + $<$:-Wno-c99-extensions> + $<$:-Wno-pedantic> +) if(APPLE) + set(CMAKE_OSX_ARCHITECTURES "arm64") + target_compile_definitions(cactus_kernels PUBLIC ACCELERATE_NEW_LAPACK) find_library(ACCELERATE_FRAMEWORK Accelerate REQUIRED) target_link_libraries(cactus_kernels PUBLIC ${ACCELERATE_FRAMEWORK}) endif() -if(CMAKE_CXX_COMPILER_ID STREQUAL "AppleClang" OR CMAKE_CXX_COMPILER_ID STREQUAL "Clang") - target_compile_options(cactus_kernels PRIVATE -Wno-c99-extensions) -elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU") - target_compile_options(cactus_kernels PRIVATE -Wno-pedantic) -endif() - -# Tests if(BUILD_TESTING OR CACTUS_BUILD_TESTS) - if(APPLE) - find_library(ACCELERATE_FRAMEWORK Accelerate REQUIRED) - endif() - set(TEST_SOURCES tests/test_elementwise.cpp tests/test_matmul.cpp @@ -79,15 +64,9 @@ if(BUILD_TESTING OR CACTUS_BUILD_TESTS) tests/test_quant.cpp tests/test_dsp.cpp ) - foreach(TEST_SRC ${TEST_SOURCES}) get_filename_component(TEST_NAME ${TEST_SRC} NAME_WE) add_executable(${TEST_NAME} ${TEST_SRC}) target_link_libraries(${TEST_NAME} PRIVATE cactus_kernels) - target_include_directories(${TEST_NAME} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) - if(APPLE) - target_link_libraries(${TEST_NAME} PRIVATE ${ACCELERATE_FRAMEWORK}) - endif() endforeach() - endif() diff --git a/cactus-kernels/cactus_kernels.h b/cactus-kernels/cactus_kernels.h index 092cb5f98..f79ef7169 100644 --- a/cactus-kernels/cactus_kernels.h +++ b/cactus-kernels/cactus_kernels.h @@ -5,7 +5,7 @@ #include #include -#include "src/threading.h" +#include "threading.h" enum class Precision { INT8, diff --git a/cactus/CMakeLists.txt b/cactus/CMakeLists.txt index e8cb2fa0d..41167424c 100644 --- a/cactus/CMakeLists.txt +++ b/cactus/CMakeLists.txt @@ -1,76 +1,57 @@ cmake_minimum_required(VERSION 3.10) project(Cactus LANGUAGES CXX) -set(CMAKE_CXX_STANDARD 20) -set(CMAKE_CXX_STANDARD_REQUIRED True) +set(CMAKE_BUILD_TYPE Release CACHE STRING "") +set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) +set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}) -if(NOT CMAKE_BUILD_TYPE) - set(CMAKE_BUILD_TYPE Release) -endif() - -set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/) -set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/) - -# Build the engine (which pulls in graph -> kernels) -set(CACTUS_ENGINE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../cactus-engine") if(NOT TARGET cactus_engine) - add_subdirectory(${CACTUS_ENGINE_DIR} ${CMAKE_CURRENT_BINARY_DIR}/cactus-engine) + add_subdirectory(${CMAKE_CURRENT_SOURCE_DIR}/../cactus-engine ${CMAKE_CURRENT_BINARY_DIR}/cactus-engine) endif() -# Produce a combined libcactus.a that contains all symbols from the three layers. -# This is what downstream consumers (tests, mobile builds) link against. +set(_ARCHIVES $ $ $) +set(_DEPS cactus_engine cactus_graph cactus_kernels) + if(APPLE) - # Use libtool to merge static archives add_custom_target(cactus ALL - COMMAND libtool -static -o ${CMAKE_BINARY_DIR}/libcactus.a - $ - $ - $ - DEPENDS cactus_engine cactus_graph cactus_kernels - COMMENT "Merging static libraries into libcactus.a" + COMMAND libtool -static -o ${CMAKE_BINARY_DIR}/libcactus.a ${_ARCHIVES} + DEPENDS ${_DEPS} ) else() - # Use ar to create a combined archive via MRI script - set(MRI_SCRIPT "${CMAKE_BINARY_DIR}/combine.mri") - file(GENERATE OUTPUT ${MRI_SCRIPT} CONTENT -"create ${CMAKE_BINARY_DIR}/libcactus.a -addlib $ -addlib $ -addlib $ -save -end -") + file(GENERATE OUTPUT ${CMAKE_BINARY_DIR}/combine.mri CONTENT + "create ${CMAKE_BINARY_DIR}/libcactus.a\naddlib $\naddlib $\naddlib $\nsave\nend\n" + ) add_custom_target(cactus ALL - COMMAND ${CMAKE_AR} -M < ${MRI_SCRIPT} - DEPENDS cactus_engine cactus_graph cactus_kernels - COMMENT "Merging static libraries into libcactus.a" + COMMAND ${CMAKE_AR} -M < ${CMAKE_BINARY_DIR}/combine.mri + DEPENDS ${_DEPS} ) endif() -file(WRITE ${CMAKE_BINARY_DIR}/cactus_shared_stub.cpp "") -add_library(cactus_shared SHARED ${CMAKE_BINARY_DIR}/cactus_shared_stub.cpp) +if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR) +file(WRITE ${CMAKE_BINARY_DIR}/stub.cpp "") +add_library(cactus_shared SHARED ${CMAKE_BINARY_DIR}/stub.cpp) set_target_properties(cactus_shared PROPERTIES OUTPUT_NAME cactus) -add_dependencies(cactus_shared cactus_engine cactus_graph cactus_kernels) +add_dependencies(cactus_shared ${_DEPS}) + if(APPLE) target_link_options(cactus_shared PRIVATE -Wl,-force_load,$ -Wl,-force_load,$ -Wl,-force_load,$ ) - set(_CACTUS_CURL_MACOS "${CMAKE_CURRENT_SOURCE_DIR}/../cactus-engine/libs/curl/macos/libcurl.a") - if(EXISTS ${_CACTUS_CURL_MACOS}) - target_link_libraries(cactus_shared PRIVATE ${_CACTUS_CURL_MACOS}) + set(_CURL "${CACTUS_CURL_ROOT}/macos/libcurl.a") + if(EXISTS "${_CURL}") + target_link_libraries(cactus_shared PRIVATE "${_CURL}") endif() target_link_libraries(cactus_shared PRIVATE - "-framework CoreML" "-framework Foundation" "-framework Accelerate" + "-framework Accelerate" "-framework CoreML" "-framework Foundation" "-framework Security" "-framework SystemConfiguration" "-framework CFNetwork" ) else() - target_link_options(cactus_shared PRIVATE - -Wl,--whole-archive - $ - $ - $ - -Wl,--no-whole-archive - ) + target_link_options(cactus_shared PRIVATE -Wl,--whole-archive ${_ARCHIVES} -Wl,--no-whole-archive) + find_package(CURL QUIET) + if(CURL_FOUND) + target_link_libraries(cactus_shared PRIVATE ${CURL_LIBRARIES}) + endif() +endif() endif() diff --git a/cactus/build.sh b/cactus/build.sh index b139b23e0..051de97c2 100755 --- a/cactus/build.sh +++ b/cactus/build.sh @@ -1,19 +1,10 @@ #!/bin/bash - set -e missing=() -if ! command -v cmake &> /dev/null; then - missing+=("cmake") -fi - -if ! command -v make &> /dev/null; then - missing+=("make") -fi - -if ! command -v g++ &> /dev/null && ! command -v clang++ &> /dev/null; then - missing+=("g++") -fi +command -v cmake &> /dev/null || missing+=("cmake") +command -v make &> /dev/null || missing+=("make") +command -v g++ &> /dev/null || command -v clang++ &> /dev/null || missing+=("g++") if [ ${#missing[@]} -gt 0 ]; then echo "Error: Missing required build tools: ${missing[*]}" @@ -22,18 +13,15 @@ if [ ${#missing[@]} -gt 0 ]; then echo "Install with: sudo apt-get install cmake build-essential" elif [[ "$OSTYPE" == "darwin"* ]]; then echo "Install with: xcode-select --install && brew install cmake" - else - echo "Please install cmake and a C++ compiler for your platform." fi exit 1 fi -echo "Building Cactus library..." +cd "$(dirname "$0")" -cd "$(dirname "$0")/../cactus" +echo "Building Cactus library..." rm -rf build - mkdir -p build cd build @@ -41,4 +29,5 @@ cmake .. -DCMAKE_RULE_MESSAGES=OFF -DCMAKE_VERBOSE_MAKEFILE=OFF > /dev/null 2>&1 make -j$(nproc 2>/dev/null || sysctl -n hw.ncpu 2>/dev/null || echo 4) echo "Cactus library built successfully!" -echo "Library location: $(pwd)/libcactus.a" +echo " Static: $(pwd)/libcactus.a" +echo " Shared: $(pwd)/libcactus.$([ "$(uname)" = Darwin ] && echo dylib || echo so)" diff --git a/cactus/cactus.h b/cactus/cactus.h index ba649661c..8d5032c5c 100644 --- a/cactus/cactus.h +++ b/cactus/cactus.h @@ -1,8 +1,8 @@ #ifndef CACTUS_H #define CACTUS_H -#include "../cactus-kernels/cactus_kernels.h" -#include "../cactus-graph/cactus_graph.h" -#include "../cactus-engine/cactus_engine.h" +#include "cactus_kernels.h" +#include "cactus_graph.h" +#include "cactus_engine.h" #endif // CACTUS_H diff --git a/docs/cactus_engine.md b/docs/cactus_engine.md index 706767ef8..d30c7e58a 100644 --- a/docs/cactus_engine.md +++ b/docs/cactus_engine.md @@ -763,105 +763,6 @@ if (result >= 0) { cactus_stream_transcribe_stop(stream, NULL, 0); ``` -### `cactus_diarize` -Runs speaker diarization on audio using the pyannote/segmentation-3.0 model. Supports both file-based and buffer-based audio input. - -```c -int cactus_diarize( - cactus_model_t model, // Model handle (must be PyAnnote model) - const char* audio_file_path, // Path to WAV file (16-bit PCM) - can be NULL if using pcm_buffer - char* response_buffer, // Buffer for response JSON - size_t buffer_size, // Size of response buffer - const char* options_json, // Optional JSON options (can be NULL) - const uint8_t* pcm_buffer, // Optional raw int16 PCM buffer (can be NULL if using file) - size_t pcm_buffer_size // Size of PCM buffer in bytes (must be even and >= 2) -); -``` - -**Returns:** Number of bytes written to response_buffer on success, negative value on error - -**Options (`options_json`):** -| Field | Type | Default | Description | -|---|---|---|---| -| `step_ms` | int | 1000 | Sliding window stride in milliseconds. Smaller = more overlap and smoother output, larger = faster. | -| `threshold` | float | none | If set, zeroes out per-speaker scores below this value. Equivalent to `segmentation.threshold` in the Python pipeline. | -| `num_speakers` | int | none | Keep only the N most active speakers (by total activity), zeroing out the rest. | -| `min_speakers` | int | none | Lower bound on the number of active speakers to retain. | -| `max_speakers` | int | none | Upper bound on the number of active speakers to retain. | -| `raw_powerset` | bool | false | Return raw 7-class powerset scores instead of 3-speaker probabilities. When true, speaker filtering and thresholding are skipped. | - -**Note:** Exactly one of `audio_file_path` or `pcm_buffer` must be provided; passing both or neither returns -1. The file path must point to a 16-bit PCM WAV file. The `pcm_buffer` must contain 16-bit signed PCM samples at 16 kHz and `pcm_buffer_size` must be even and at least 2. - -The model processes 10-second windows (160,000 samples at 16 kHz) with configurable step. Shorter input is zero-padded. Output scores are a flat array of T × num_speakers float32 values in row-major order (index `f*num_speakers+s`), where T is the total number of output frames. When `raw_powerset` is false (default), num_speakers is 3 and each value is the Hamming-weighted mean of per-speaker probabilities in [0, 1]. When `raw_powerset` is true, num_speakers is 7 and values are raw powerset class scores. - -**Response Format:** -```json -{ - "success": true, - "error": null, - "num_speakers": 3, - "scores": [0.0, 0.1, ...], - "total_time_ms": 12.34, - "ram_usage_mb": 256.0 -} -``` - -**Example:** -```c -cactus_model_t pyannote = cactus_init("../../weights/segmentation-3.0", NULL, false); - -char response[1 << 20]; -int result = cactus_diarize(pyannote, "audio.wav", response, sizeof(response), "{\"step_ms\":500}", NULL, 0); - -if (result >= 0) { - printf("Response: %s\n", response); -} -``` - -### `cactus_embed_speaker` -Extracts a speaker embedding vector from audio using the WeSpeaker ResNet34-LM model. Supports both file-based and buffer-based audio input. Filter bank features are computed internally from raw audio. - -```c -int cactus_embed_speaker( - cactus_model_t model, // Model handle (must be WeSpeaker model) - const char* audio_file_path, // Path to WAV file (16-bit PCM) - can be NULL if using pcm_buffer - char* response_buffer, // Buffer for response JSON - size_t buffer_size, // Size of response buffer - const char* options_json, // Optional JSON options (can be NULL, reserved for future use) - const uint8_t* pcm_buffer, // Optional raw int16 PCM buffer (can be NULL if using file) - size_t pcm_buffer_size, // Size of PCM buffer in bytes (must be even and >= 2) - const float* mask_weights, // Optional per-frame mask weights for weighted embedding (can be NULL) - size_t mask_num_frames // Number of mask weight frames (0 if mask_weights is NULL) -); -``` - -**Returns:** Number of bytes written to response_buffer on success, negative value on error - -**Note:** Exactly one of `audio_file_path` or `pcm_buffer` must be provided; passing both or neither returns -1. The file path must point to a 16-bit PCM WAV file. The `pcm_buffer` must contain 16-bit signed PCM samples at 16 kHz and `pcm_buffer_size` must be even and at least 2. Output is a 256-dimensional speaker embedding. When `mask_weights` is provided, weighted stats pooling is used to extract a speaker-specific embedding — the mask weights are resampled to match the model's internal temporal resolution. - -**Response Format:** -```json -{ - "success": true, - "error": null, - "embedding": [0.123, -0.456, ...], - "total_time_ms": 8.12, - "ram_usage_mb": 128.0 -} -``` - -**Example:** -```c -cactus_model_t wespeaker = cactus_init("../../weights/wespeaker-voxceleb-resnet34-lm", NULL, false); - -char response[1 << 16]; -int result = cactus_embed_speaker(wespeaker, "audio.wav", response, sizeof(response), NULL, NULL, 0, NULL, 0); - -if (result >= 0) { - printf("Response: %s\n", response); -} -``` - ### `cactus_detect_language` Detects the spoken language in an audio file or PCM buffer. @@ -926,82 +827,6 @@ if (result >= 0) { } ``` -### `cactus_vad` -Detects speech segments in audio using Voice Activity Detection. Supports both file-based and buffer-based audio input. - -```c -int cactus_vad( - cactus_model_t model, // Model handle (must be VAD model) - const char* audio_file_path, // Path to WAV file (16-bit PCM) - can be NULL if using pcm_buffer - char* response_buffer, // Buffer for response JSON - size_t buffer_size, // Size of response buffer - const char* options_json, // Optional VAD options (can be NULL) - const uint8_t* pcm_buffer, // Optional raw PCM audio buffer (can be NULL if using file) - size_t pcm_buffer_size // Size of PCM buffer in bytes (must be even and >= 2) -); -``` - -**Returns:** Number of bytes written to response_buffer on success, negative value on error - -**Note:** Exactly one of `audio_file_path` or `pcm_buffer` must be provided; passing both or neither returns -1. The file path must point to a 16-bit PCM WAV file. The `pcm_buffer` must contain 16-bit signed PCM samples at 16 kHz and `pcm_buffer_size` must be even and at least 2. - -**Options Format:** -```json -{ - "threshold": 0.5, - "neg_threshold": 0.0, - "min_speech_duration_ms": 250, - "max_speech_duration_s": 30.0, - "min_silence_duration_ms": 100, - "speech_pad_ms": 30, - "window_size_samples": 512, - "min_silence_at_max_speech": 98, - "use_max_poss_sil_at_max_speech": true, - "sampling_rate": 16000 -} -``` - -| Option | Type | Default | Description | -|--------|------|---------|-------------| -| `threshold` | float | 0.5 | Speech probability threshold (0.0–1.0) | -| `neg_threshold` | float | 0.0 | Threshold below which a frame is considered non-speech; 0.0 means auto-compute as `max(threshold - 0.15, 0.01)` | -| `min_speech_duration_ms` | int | 250 | Minimum speech segment duration in milliseconds | -| `max_speech_duration_s` | float | infinity | Maximum speech segment duration in seconds | -| `min_silence_duration_ms` | int | 100 | Minimum silence duration to split segments | -| `speech_pad_ms` | int | 30 | Padding added to each end of a speech segment in milliseconds | -| `window_size_samples` | int | 512 | Window size for VAD processing | -| `min_silence_at_max_speech` | int | 98 | Minimum silence duration in milliseconds at which a segment may be split when max_speech_duration_s is reached | -| `use_max_poss_sil_at_max_speech` | bool | true | Use maximum possible silence at max speech duration | -| `sampling_rate` | int | 16000 | Audio sampling rate in Hz | - -**Response Format:** -```json -{ - "success": true, - "error": null, - "segments": [ - {"start": 0, "end": 16000}, - {"start": 32000, "end": 48000} - ], - "total_time_ms": 12.34, - "ram_usage_mb": 45.67 -} -``` - -- `segments`: Array of `{"start": int, "end": int}` objects, where values are sample indices (not seconds) - -**Example:** -```c -cactus_model_t vad = cactus_init("../../weights/silero-vad", NULL, false); - -char response[4096]; -int result = cactus_vad(vad, "audio.wav", response, sizeof(response), NULL, NULL, 0); - -if (result >= 0) { - printf("Response: %s\n", response); -} -``` - ### `cactus_embed` Generates text embeddings for semantic search, similarity, and RAG applications. diff --git a/docs/choose-bindings.md b/docs/choose-bindings.md index 78789f27c..c44820671 100644 --- a/docs/choose-bindings.md +++ b/docs/choose-bindings.md @@ -13,7 +13,6 @@ Pick the right one for your platform and use case: | **Vision** | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | | **Audio** | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | | **Transcription** | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | -| **Diarization** | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | | **Function Calling** | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | | **RAG / Embeddings** | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | | **Cloud Fallback** | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | :white_check_mark: | diff --git a/python/README.md b/python/README.md index dba156deb..a8ba95fd7 100644 --- a/python/README.md +++ b/python/README.md @@ -257,54 +257,6 @@ result_json = cactus_detect_language( Returns a JSON string with fields: `success`, `error`, `language` (BCP-47 code), `language_token`, `token_id`, `confidence`, `entropy`, `total_time_ms`, `ram_usage_mb`. -### VAD - -```python -result_json = cactus_vad( - model: int, - audio_path: str | None, - options_json: str | None, - pcm_data: bytes | None -) -> str -``` - -Returns a JSON string: `{"success":true,"error":null,"segments":[{"start":,"end":},...],"total_time_ms":...,"ram_usage_mb":...}`. VAD segments contain only `start` and `end` as integer sample indices — no `text` field. - -### Diarize - -```python -result_json = cactus_diarize( - model: int, - audio_path: str | None, - options_json: str | None, - pcm_data: bytes | None -) -> str -``` - -Options (all optional): -- `step_ms` (int, default 1000) — sliding window stride in milliseconds -- `threshold` (float) — zero out per-speaker scores below this value (`segmentation.threshold` in Python pipeline) -- `num_speakers` (int) — keep only the N most active speakers -- `min_speakers` (int) — minimum number of speakers to retain -- `max_speakers` (int) — maximum number of speakers to retain -- `raw_powerset` (bool, default false) — return raw 7-class powerset scores instead of 3-speaker probabilities - -Returns `{"success":true,"error":null,"num_speakers":3,"scores":[...],"total_time_ms":...,"ram_usage_mb":...}`. The `scores` field is a flat array of T×3 float32 values (index `f*3+s`), one per output frame per speaker, each in [0,1]. When `raw_powerset` is true, `num_speakers` is 7 and `scores` contains T×7 raw powerset class scores (speaker filtering and thresholding are skipped). - -### Embed Speaker - -```python -result_json = cactus_embed_speaker( - model: int, - audio_path: str | None, - options_json: str | None, - pcm_data: bytes | None, - mask_weights: list[float] | None = None -) -> str -``` - -Returns a JSON string: `{"success":true,"error":null,"embedding":[, ...],"total_time_ms":...,"ram_usage_mb":...}`. The embedding is a 256-dimensional speaker vector from the WeSpeaker ResNet34-LM model. When `mask_weights` is provided (a per-frame weight array from diarization), the embedding is extracted using weighted stats pooling for speaker-specific embeddings. - ### RAG ```python diff --git a/python/cactus/__init__.py b/python/cactus/__init__.py index f97cef0b9..83b6e54d2 100644 --- a/python/cactus/__init__.py +++ b/python/cactus/__init__.py @@ -1,38 +1,77 @@ """Cactus — on-device AI inference.""" -__version__ = "1.14.0" +from importlib.metadata import version as _version, PackageNotFoundError +from pathlib import Path as _Path -from .cli.download import ensure_model, get_weights_dir, get_model_dir_name -from .cli import main +def _read_version(): + try: + return _version("cactus-compute") + except PackageNotFoundError: + pass + vfile = _Path(__file__).resolve().parent.parent.parent / "CACTUS_VERSION" + if vfile.exists(): + raw = vfile.read_text().strip().lstrip("v") + return raw + ".0" if raw.count(".") == 1 else raw + return "0.0.0" + +__version__ = _read_version() + +from .cli.download import ensure_model, get_weights_dir, get_model_dir_name +from .cli import main -_FFI_NAMES = frozenset({ - "cactus_init", "cactus_destroy", "cactus_reset", "cactus_stop", - "cactus_complete", "cactus_prefill", - "cactus_embed", "cactus_image_embed", "cactus_audio_embed", - "cactus_transcribe", "cactus_detect_language", - "cactus_vad", "cactus_diarize", "cactus_embed_speaker", - "cactus_tokenize", "cactus_decode_tokens", "cactus_score_window", +__all__ = [ + "__version__", + "ensure_model", + "get_weights_dir", + "get_model_dir_name", + "main", + "Graph", + "Tensor", + "cactus_init", + "cactus_destroy", + "cactus_reset", + "cactus_stop", + "cactus_complete", + "cactus_prefill", + "cactus_embed", + "cactus_image_embed", + "cactus_audio_embed", + "cactus_transcribe", + "cactus_detect_language", + "cactus_tokenize", + "cactus_decode_tokens", + "cactus_score_window", "cactus_rag_query", - "cactus_stream_transcribe_start", "cactus_stream_transcribe_process", + "cactus_stream_transcribe_start", + "cactus_stream_transcribe_process", "cactus_stream_transcribe_stop", - "cactus_index_init", "cactus_index_add", "cactus_index_delete", - "cactus_index_query", "cactus_index_get", "cactus_index_compact", + "cactus_index_init", + "cactus_index_add", + "cactus_index_delete", + "cactus_index_query", + "cactus_index_get", + "cactus_index_compact", "cactus_index_destroy", - "cactus_set_app_id", "cactus_set_telemetry_environment", - "cactus_telemetry_flush", "cactus_telemetry_shutdown", - "cactus_log_set_level", "cactus_log_set_callback", - "cactus_get_last_error", "cactus_preprocess_audio_features", -}) + "cactus_set_app_id", + "cactus_set_telemetry_environment", + "cactus_telemetry_flush", + "cactus_telemetry_shutdown", + "cactus_log_set_level", + "cactus_log_set_callback", + "cactus_get_last_error", + "cactus_preprocess_audio_features", +] + +_FFI_NAMES = frozenset(n for n in __all__ if n.startswith("cactus_")) + def __getattr__(name): if name in ("Graph", "Tensor"): from .bindings import graph return getattr(graph, name) - if name in _FFI_NAMES: from .bindings import cactus return getattr(cactus, name) - raise AttributeError(f"module {__name__!r} has no attribute {name!r}") diff --git a/python/cactus/bindings/cactus.py b/python/cactus/bindings/cactus.py index a10ea10e0..b25731405 100644 --- a/python/cactus/bindings/cactus.py +++ b/python/cactus/bindings/cactus.py @@ -1,26 +1,36 @@ """Cactus Python FFI bindings.""" import ctypes +import json import os import platform from pathlib import Path TokenCallback = ctypes.CFUNCTYPE(None, ctypes.c_char_p, ctypes.c_uint32, ctypes.c_void_p) -_DIR = Path(__file__).parent.parent.parent.parent -_OVERRIDE_LIB_PATH = os.environ.get("CACTUS_LIB_PATH") -if _OVERRIDE_LIB_PATH: - _LIB_PATH = Path(_OVERRIDE_LIB_PATH).expanduser().resolve() -elif platform.system() == "Darwin": - _LIB_PATH = _DIR / "cactus" / "build" / "libcactus.dylib" -else: - _LIB_PATH = _DIR / "cactus" / "build" / "libcactus.so" +_LIB_NAME = "libcactus.dylib" if platform.system() == "Darwin" else "libcactus.so" + + +def _find_library(): + override = os.environ.get("CACTUS_LIB_PATH") + if override: + return Path(override).expanduser().resolve() + + bundled = Path(__file__).parent / "lib" / _LIB_NAME + if bundled.exists(): + return bundled + + dev_build = Path(__file__).parent.parent.parent.parent / "cactus" / "build" / _LIB_NAME + if dev_build.exists(): + return dev_build -if not _LIB_PATH.exists(): raise RuntimeError( - f"Cactus library not found at {_LIB_PATH}\n" - f"Please build first: cactus build --python" + f"Cactus library ({_LIB_NAME}) not found.\n" + f"Install with: pip install cactus-compute\n" + f"Or build from source: cactus build --python" ) + +_LIB_PATH = _find_library() _lib = ctypes.CDLL(str(_LIB_PATH)) @@ -666,33 +676,6 @@ class cactus_tensor_info_t(ctypes.Structure): except AttributeError: pass -_bind_optional( - "cactus_vad", - [ - ctypes.c_void_p, ctypes.c_char_p, ctypes.c_char_p, ctypes.c_size_t, - ctypes.c_char_p, ctypes.POINTER(ctypes.c_uint8), ctypes.c_size_t, - ], - ctypes.c_int, -) - -_bind_optional( - "cactus_diarize", - [ - ctypes.c_void_p, ctypes.c_char_p, ctypes.c_char_p, ctypes.c_size_t, - ctypes.c_char_p, ctypes.POINTER(ctypes.c_uint8), ctypes.c_size_t, - ], - ctypes.c_int, -) - -_bind_optional( - "cactus_embed_speaker", - [ - ctypes.c_void_p, ctypes.c_char_p, ctypes.c_char_p, ctypes.c_size_t, - ctypes.c_char_p, ctypes.POINTER(ctypes.c_uint8), ctypes.c_size_t, - ctypes.POINTER(ctypes.c_float), ctypes.c_size_t, - ], - ctypes.c_int, -) _lib.cactus_reset.argtypes = [ctypes.c_void_p] _lib.cactus_reset.restype = None @@ -836,13 +819,50 @@ class cactus_tensor_info_t(ctypes.Structure): def _enc(s): + """Encode a string to bytes for C, or pass through None/bytes.""" if s is None: return None return s.encode() if isinstance(s, str) else s +def _to_json(obj): + """Accept str, bytes, dict, list, or None — return encoded bytes for C.""" + if obj is None: + return None + if isinstance(obj, (dict, list)): + return json.dumps(obj).encode() + if isinstance(obj, str): + return obj.encode() + return obj + + +def _from_json(buf): + """Decode a ctypes string buffer to a dict. Returns {} on empty.""" + text = buf.value.decode("utf-8", errors="ignore") + if not text: + return {} + return json.loads(text) + + +def _prepare_pcm(pcm_data): + """Marshal pcm_data bytes to (ctypes_ptr, size) for C. Returns (None, 0) if None.""" + if pcm_data is None: + return None, 0 + pcm_arr = (ctypes.c_uint8 * len(pcm_data))(*pcm_data) + return ctypes.cast(pcm_arr, ctypes.POINTER(ctypes.c_uint8)), len(pcm_data) + + +def _make_token_callback(callback): + """Wrap a Python callback(text, token_id) into a C-compatible TokenCallback.""" + if not callback: + return TokenCallback() + def _bridge(token_bytes, token_id, _): + callback(token_bytes.decode("utf-8", errors="ignore") if token_bytes else "", token_id) + return TokenCallback(_bridge) + + def cactus_get_last_error(): - """Returns the last error message, or None if no error.""" + """Returns the last error message from the C runtime, or None.""" result = _lib.cactus_get_last_error() return result.decode() if result else None @@ -851,6 +871,9 @@ def _err(default): return cactus_get_last_error() or default +# ── Telemetry ──────────────────────────────────────────────────────── + + def cactus_set_telemetry_environment(framework, cache_location, version): """Sets telemetry environment (framework name, cache path, version).""" _lib.cactus_set_telemetry_environment(_enc(framework), _enc(cache_location), _enc(version)) @@ -871,8 +894,21 @@ def cactus_telemetry_shutdown(): _lib.cactus_telemetry_shutdown() -def cactus_init(model_path, corpus_dir, cache_index): - """Loads a model from the given path. Returns an opaque model handle.""" +# ── Model lifecycle ────────────────────────────────────────────────── + + +def cactus_init(model_path, corpus_dir=None, cache_index=False): + """Load a model from disk. + + Args: + model_path: Path to the converted model weights directory. + corpus_dir: Optional path to a RAG corpus directory. + cache_index: Whether to cache the RAG index to disk. + + Returns: + An opaque model handle. Pass to other cactus_* functions. + Call cactus_destroy() when done. + """ handle = _lib.cactus_init(_enc(model_path), _enc(corpus_dir), cache_index) if not handle: raise RuntimeError(_err("Failed to initialize model")) @@ -880,108 +916,193 @@ def cactus_init(model_path, corpus_dir, cache_index): def cactus_destroy(model): - """Frees all resources associated with the model handle.""" + """Free all resources associated with a model handle.""" _lib.cactus_destroy(model) def cactus_reset(model): - """Clears the KV cache, resetting the model to a fresh state.""" + """Clear the KV cache, resetting the model to a fresh conversation state.""" _lib.cactus_reset(model) def cactus_stop(model): - """Signals the model to stop the current generation.""" + """Signal the model to stop the current generation early.""" _lib.cactus_stop(model) -def cactus_complete(model, messages_json, options_json, tools_json, callback, pcm_data=None): - """Runs chat completion. Returns a JSON string with the response and metrics.""" +# ── LLM completion ─────────────────────────────────────────────────── + + +def cactus_complete(model, messages, options=None, tools=None, callback=None, pcm_data=None): + """Run chat completion. + + Args: + model: Model handle from cactus_init(). + messages: List of message dicts, e.g. [{"role": "user", "content": "Hi"}]. + Also accepts a pre-serialized JSON string. + options: Optional dict of generation options (temperature, max_tokens, etc.). + tools: Optional list of tool definitions for function calling. + callback: Optional function(text, token_id) called on each generated token. + pcm_data: Optional raw PCM audio bytes for audio-capable models. + + Returns: + A dict with the completion response and metrics. + """ buf = ctypes.create_string_buffer(65536) - if callback: - def _bridge(token_bytes, token_id, _): - callback(token_bytes.decode("utf-8", errors="ignore") if token_bytes else "", token_id) - cb = TokenCallback(_bridge) - else: - cb = TokenCallback() - if pcm_data is not None: - pcm_arr = (ctypes.c_uint8 * len(pcm_data))(*pcm_data) - pcm_ptr = ctypes.cast(pcm_arr, ctypes.POINTER(ctypes.c_uint8)) - pcm_size = len(pcm_data) - else: - pcm_ptr = None - pcm_size = 0 + cb = _make_token_callback(callback) + pcm_ptr, pcm_size = _prepare_pcm(pcm_data) rc = _lib.cactus_complete( - model, _enc(messages_json), buf, len(buf), - _enc(options_json), _enc(tools_json), cb, None, pcm_ptr, pcm_size + model, _to_json(messages), buf, len(buf), + _to_json(options), _to_json(tools), cb, None, pcm_ptr, pcm_size, ) if rc < 0: raise RuntimeError(_err("Completion failed")) - return buf.value.decode("utf-8", errors="ignore") + return _from_json(buf) + +def cactus_prefill(model, messages, options=None, tools=None, pcm_data=None): + """Pre-fill the KV cache with messages without generating a response. -def cactus_prefill(model, messages_json, options_json, tools_json, pcm_data=None): - """Pre-fills the KV cache with the given messages. Returns a JSON string with prefill stats.""" + Returns: + A dict with prefill stats (tokens processed, latency, etc.). + """ buf = ctypes.create_string_buffer(65536) - if pcm_data is not None: - pcm_arr = (ctypes.c_uint8 * len(pcm_data))(*pcm_data) - pcm_ptr = ctypes.cast(pcm_arr, ctypes.POINTER(ctypes.c_uint8)) - pcm_size = len(pcm_data) - else: - pcm_ptr = None - pcm_size = 0 + pcm_ptr, pcm_size = _prepare_pcm(pcm_data) rc = _lib.cactus_prefill( - model, _enc(messages_json), buf, len(buf), - _enc(options_json), _enc(tools_json), pcm_ptr, pcm_size + model, _to_json(messages), buf, len(buf), + _to_json(options), _to_json(tools), pcm_ptr, pcm_size, ) if rc < 0: raise RuntimeError(_err("Prefill failed")) - return buf.value.decode("utf-8", errors="replace") + return _from_json(buf) + + +# ── Audio / speech ─────────────────────────────────────────────────── + -def cactus_detect_language(model, audio_path, options_json, pcm_data): - """Detects the spoken language in audio. Returns a JSON string.""" +def cactus_transcribe(model, audio_path, prompt=None, options=None, callback=None, pcm_data=None): + """Transcribe audio to text. + + Args: + model: Model handle. + audio_path: Path to a WAV audio file. + prompt: Optional prompt to guide transcription. + options: Optional dict of transcription options. + callback: Optional function(text, token_id) for streaming tokens. + pcm_data: Optional raw PCM audio bytes (alternative to audio_path). + + Returns: + A dict with transcription text and segments. + """ buf = ctypes.create_string_buffer(65536) - if pcm_data is not None: - pcm_arr = (ctypes.c_uint8 * len(pcm_data))(*pcm_data) - pcm_ptr = ctypes.cast(pcm_arr, ctypes.POINTER(ctypes.c_uint8)) - pcm_size = len(pcm_data) - else: - pcm_ptr = None - pcm_size = 0 + cb = _make_token_callback(callback) + pcm_ptr, pcm_size = _prepare_pcm(pcm_data) + rc = _lib.cactus_transcribe( + model, _enc(audio_path), _enc(prompt), buf, len(buf), + _to_json(options), cb, None, pcm_ptr, pcm_size, + ) + if rc < 0: + raise RuntimeError(_err("Transcription failed")) + return _from_json(buf) + + +def cactus_detect_language(model, audio_path, options=None, pcm_data=None): + """Detect the spoken language in audio. + + Returns: + A dict with detected language info. + """ + buf = ctypes.create_string_buffer(65536) + pcm_ptr, pcm_size = _prepare_pcm(pcm_data) rc = _lib.cactus_detect_language( - model, _enc(audio_path), buf, len(buf), _enc(options_json), pcm_ptr, pcm_size + model, _enc(audio_path), buf, len(buf), _to_json(options), pcm_ptr, pcm_size, ) if rc < 0: raise RuntimeError(_err("Detect language failed")) - return buf.value.decode("utf-8", errors="ignore") + return _from_json(buf) -def cactus_transcribe(model, audio_path, prompt, options_json, callback, pcm_data): - """Transcribes audio to text. Returns a JSON string with transcription and segments.""" - buf = ctypes.create_string_buffer(65536) - if callback: - def _bridge(token_bytes, token_id, _): - callback(token_bytes.decode("utf-8", errors="ignore") if token_bytes else "", token_id) - cb = TokenCallback(_bridge) - else: - cb = TokenCallback() - if pcm_data is not None: - pcm_arr = (ctypes.c_uint8 * len(pcm_data))(*pcm_data) - pcm_ptr = ctypes.cast(pcm_arr, ctypes.POINTER(ctypes.c_uint8)) - pcm_size = len(pcm_data) - else: - pcm_ptr = None - pcm_size = 0 - rc = _lib.cactus_transcribe( - model, _enc(audio_path), _enc(prompt), buf, len(buf), - _enc(options_json), cb, None, pcm_ptr, pcm_size +def cactus_preprocess_audio_features(audio_path, model_type, mel_bins, capacity): + """Compute mel spectrogram features from an audio file. + + Returns: + A tuple (values, mel_bins, frames) where values is a list of floats. + """ + if not hasattr(_lib, "cactus_preprocess_audio_features"): + raise RuntimeError("cactus_preprocess_audio_features is unavailable; rebuild with cactus build --python") + buf = (ctypes.c_float * int(capacity))() + feature_count = ctypes.c_size_t() + out_mels = ctypes.c_size_t() + out_frames = ctypes.c_size_t() + rc = _lib.cactus_preprocess_audio_features( + _enc(audio_path), _enc(model_type), + ctypes.c_size_t(int(mel_bins)), + buf, ctypes.sizeof(buf), + ctypes.byref(feature_count), ctypes.byref(out_mels), ctypes.byref(out_frames), ) if rc < 0: - raise RuntimeError(_err("Transcription failed")) - return buf.value.decode("utf-8", errors="ignore") + raise RuntimeError(_err("Audio feature preprocessing failed")) + return list(buf[:feature_count.value]), int(out_mels.value), int(out_frames.value) + + +# ── Streaming transcription ────────────────────────────────────────── + +def cactus_stream_transcribe_start(model, options=None): + """Start a streaming transcription session. -def cactus_embed(model, text, normalize): - """Computes a text embedding. Returns a list of floats.""" + Returns: + An opaque stream handle. Feed audio with cactus_stream_transcribe_process(), + then finalize with cactus_stream_transcribe_stop(). + """ + handle = _lib.cactus_stream_transcribe_start(model, _to_json(options)) + if not handle: + raise RuntimeError(_err("Failed to start stream transcription")) + return handle + + +def cactus_stream_transcribe_process(stream, pcm_data): + """Feed a PCM audio chunk to a streaming transcription session. + + Returns: + A dict with partial transcription results. + """ + buf = ctypes.create_string_buffer(65536) + pcm_arr = (ctypes.c_uint8 * len(pcm_data))(*pcm_data) + pcm_ptr = ctypes.cast(pcm_arr, ctypes.POINTER(ctypes.c_uint8)) + rc = _lib.cactus_stream_transcribe_process(stream, pcm_ptr, len(pcm_data), buf, len(buf)) + if rc < 0: + raise RuntimeError(_err("Stream process failed")) + return _from_json(buf) + + +def cactus_stream_transcribe_stop(stream): + """Finalize a streaming transcription session. + + Returns: + A dict with the final transcription. + """ + buf = ctypes.create_string_buffer(65536) + rc = _lib.cactus_stream_transcribe_stop(stream, buf, len(buf)) + if rc < 0: + raise RuntimeError(_err("Stream stop failed")) + return _from_json(buf) + + +# ── Embeddings ─────────────────────────────────────────────────────── + + +def cactus_embed(model, text, normalize=True): + """Compute a text embedding. + + Args: + model: Model handle. + text: The text to embed. + normalize: Whether to L2-normalize the embedding (default True). + + Returns: + A list of floats (the embedding vector). + """ buf = (ctypes.c_float * 4096)() dim = ctypes.c_size_t() rc = _lib.cactus_embed(model, _enc(text), buf, 4096, ctypes.byref(dim), normalize) @@ -991,7 +1112,7 @@ def cactus_embed(model, text, normalize): def cactus_image_embed(model, image_path): - """Computes an image embedding. Returns a list of floats.""" + """Compute an image embedding. Returns a list of floats.""" buf = (ctypes.c_float * 4096)() dim = ctypes.c_size_t() rc = _lib.cactus_image_embed(model, _enc(image_path), buf, 4096, ctypes.byref(dim)) @@ -1001,7 +1122,7 @@ def cactus_image_embed(model, image_path): def cactus_audio_embed(model, audio_path): - """Computes an audio embedding. Returns a list of floats.""" + """Compute an audio embedding. Returns a list of floats.""" buf = (ctypes.c_float * 4096)() dim = ctypes.c_size_t() rc = _lib.cactus_audio_embed(model, _enc(audio_path), buf, 4096, ctypes.byref(dim)) @@ -1010,93 +1131,11 @@ def cactus_audio_embed(model, audio_path): return list(buf[:dim.value]) -def cactus_preprocess_audio_features(audio_path, model_type, mel_bins, capacity): - """Computes mel spectrogram features. Returns (values, mel_bins, frames).""" - if not hasattr(_lib, "cactus_preprocess_audio_features"): - raise RuntimeError("cactus_preprocess_audio_features is unavailable; rebuild with cactus build --python") - buf = (ctypes.c_float * int(capacity))() - feature_count = ctypes.c_size_t() - out_mels = ctypes.c_size_t() - out_frames = ctypes.c_size_t() - rc = _lib.cactus_preprocess_audio_features( - _enc(audio_path), - _enc(model_type), - ctypes.c_size_t(int(mel_bins)), - buf, - ctypes.sizeof(buf), - ctypes.byref(feature_count), - ctypes.byref(out_mels), - ctypes.byref(out_frames), - ) - if rc < 0: - raise RuntimeError(_err("Audio feature preprocessing failed")) - return list(buf[:feature_count.value]), int(out_mels.value), int(out_frames.value) - - -def cactus_vad(model, audio_path, options_json, pcm_data): - """Runs voice activity detection. Returns a JSON string with speech segments.""" - buf = ctypes.create_string_buffer(65536) - if pcm_data is not None: - pcm_arr = (ctypes.c_uint8 * len(pcm_data))(*pcm_data) - pcm_ptr = ctypes.cast(pcm_arr, ctypes.POINTER(ctypes.c_uint8)) - pcm_size = len(pcm_data) - else: - pcm_ptr = None - pcm_size = 0 - rc = _lib.cactus_vad( - model, _enc(audio_path), buf, len(buf), _enc(options_json), pcm_ptr, pcm_size - ) - if rc < 0: - raise RuntimeError(_err("VAD failed")) - return buf.value.decode("utf-8", errors="ignore") - - -def cactus_diarize(model, audio_path, options_json, pcm_data): - """Runs speaker diarization. Returns a JSON string with per-speaker segments.""" - buf = ctypes.create_string_buffer(1 << 20) - if pcm_data is not None: - pcm_arr = (ctypes.c_uint8 * len(pcm_data))(*pcm_data) - pcm_ptr = ctypes.cast(pcm_arr, ctypes.POINTER(ctypes.c_uint8)) - pcm_size = len(pcm_data) - else: - pcm_ptr = None - pcm_size = 0 - rc = _lib.cactus_diarize( - model, _enc(audio_path), buf, len(buf), _enc(options_json), pcm_ptr, pcm_size - ) - if rc < 0: - raise RuntimeError(_err("Diarize failed")) - return buf.value.decode("utf-8", errors="ignore") - - -def cactus_embed_speaker(model, audio_path, options_json, pcm_data, mask_weights=None): - """Extracts a speaker embedding. Returns a JSON string.""" - buf = ctypes.create_string_buffer(65536) - if pcm_data is not None: - pcm_arr = (ctypes.c_uint8 * len(pcm_data))(*pcm_data) - pcm_ptr = ctypes.cast(pcm_arr, ctypes.POINTER(ctypes.c_uint8)) - pcm_size = len(pcm_data) - else: - pcm_ptr = None - pcm_size = 0 - if mask_weights is not None: - mask_arr = (ctypes.c_float * len(mask_weights))(*mask_weights) - mask_ptr = ctypes.cast(mask_arr, ctypes.POINTER(ctypes.c_float)) - mask_size = len(mask_weights) - else: - mask_ptr = None - mask_size = 0 - rc = _lib.cactus_embed_speaker( - model, _enc(audio_path), buf, len(buf), _enc(options_json), - pcm_ptr, pcm_size, mask_ptr, mask_size - ) - if rc < 0: - raise RuntimeError(_err("EmbedSpeaker failed")) - return buf.value.decode("utf-8", errors="ignore") +# ── Tokenization ───────────────────────────────────────────────────── def cactus_tokenize(model, text): - """Tokenizes text into token IDs. Returns a list of ints.""" + """Tokenize text into token IDs. Returns a list of ints.""" max_tokens = 8192 arr = (ctypes.c_uint32 * max_tokens)() n = ctypes.c_size_t(0) @@ -1107,88 +1146,86 @@ def cactus_tokenize(model, text): def cactus_decode_tokens(model, tokens, temperature=0.0, top_p=1.0, top_k=1): - """Decodes the next token from a sequence. Returns the next token ID.""" + """Decode the next token from a sequence. Returns the next token ID as an int.""" if not tokens: raise ValueError("tokens must be non-empty") - arr = (ctypes.c_uint32 * len(tokens))(*[int(token) for token in tokens]) + arr = (ctypes.c_uint32 * len(tokens))(*[int(t) for t in tokens]) out = ctypes.c_uint32(0) rc = _lib.cactus_decode_tokens( - model, - arr, - len(tokens), + model, arr, len(tokens), ctypes.c_float(float(temperature)), ctypes.c_float(float(top_p)), ctypes.c_size_t(int(top_k)), ctypes.byref(out), ) if rc < 0: - raise RuntimeError(_err("Raw token decode failed")) + raise RuntimeError(_err("Token decode failed")) return int(out.value) def cactus_score_window(model, tokens, start, end, context): - """Scores a window of tokens. Returns a JSON string with log-probabilities.""" + """Score a window of tokens for log-probabilities. + + Returns: + A dict with token-level log-probability scores. + """ buf = ctypes.create_string_buffer(65536) - token_len = len(tokens) - arr = (ctypes.c_uint32 * token_len)(*tokens) - rc = _lib.cactus_score_window(model, arr, token_len, start, end, context, buf, len(buf)) + arr = (ctypes.c_uint32 * len(tokens))(*tokens) + rc = _lib.cactus_score_window(model, arr, len(tokens), start, end, context, buf, len(buf)) if rc < 0: raise RuntimeError(_err("Score window failed")) - return buf.value.decode("utf-8", errors="ignore") + return _from_json(buf) -def cactus_rag_query(model, query, top_k): - """Queries the RAG corpus. Returns a JSON string with results.""" - buf = ctypes.create_string_buffer(65536) - rc = _lib.cactus_rag_query(model, _enc(query), buf, len(buf), top_k) - if rc < 0: - raise RuntimeError(_err("RAG query failed")) - return buf.value.decode("utf-8", errors="ignore") - +# ── RAG ────────────────────────────────────────────────────────────── -def cactus_stream_transcribe_start(model, options_json): - """Starts a streaming transcription session. Returns a stream handle.""" - handle = _lib.cactus_stream_transcribe_start(model, _enc(options_json)) - if not handle: - raise RuntimeError(_err("Failed to start stream transcription")) - return handle +def cactus_rag_query(model, query, top_k=5): + """Query the RAG corpus for relevant documents. -def cactus_stream_transcribe_process(stream, pcm_data): - """Feeds a PCM audio chunk to the stream. Returns partial transcription.""" + Returns: + A dict with ranked results. + """ buf = ctypes.create_string_buffer(65536) - pcm_arr = (ctypes.c_uint8 * len(pcm_data))(*pcm_data) - pcm_ptr = ctypes.cast(pcm_arr, ctypes.POINTER(ctypes.c_uint8)) - rc = _lib.cactus_stream_transcribe_process(stream, pcm_ptr, len(pcm_data), buf, len(buf)) + rc = _lib.cactus_rag_query(model, _enc(query), buf, len(buf), top_k) if rc < 0: - raise RuntimeError(_err("Stream process failed")) - return buf.value.decode("utf-8", errors="ignore") + raise RuntimeError(_err("RAG query failed")) + return _from_json(buf) -def cactus_stream_transcribe_stop(stream): - """Finalizes streaming transcription. Returns the final transcription.""" - buf = ctypes.create_string_buffer(65536) - rc = _lib.cactus_stream_transcribe_stop(stream, buf, len(buf)) - if rc < 0: - raise RuntimeError(_err("Stream stop failed")) - return buf.value.decode("utf-8", errors="ignore") +# ── Vector index ───────────────────────────────────────────────────── def cactus_index_init(index_dir, embedding_dim): - """Creates a vector index. Returns an opaque index handle.""" + """Create a vector index for semantic search. + + Args: + index_dir: Directory to persist the index. + embedding_dim: Dimensionality of the embedding vectors. + + Returns: + An opaque index handle. Call cactus_index_destroy() when done. + """ handle = _lib.cactus_index_init(_enc(index_dir), embedding_dim) if not handle: raise RuntimeError(_err("Failed to initialize index")) return handle -def cactus_index_add(index, ids, documents, metadatas, embeddings): - """Adds documents with embeddings to the index.""" +def cactus_index_add(index, ids, documents, metadatas=None, embeddings=None): + """Add documents with embeddings to the index. + + Args: + index: Index handle from cactus_index_init(). + ids: List of integer document IDs. + documents: List of document strings. + metadatas: Optional list of metadata strings (one per document). + embeddings: List of embedding vectors (list of floats each). + """ count = len(ids) embedding_dim = len(embeddings[0]) if embeddings else 0 ids_arr = (ctypes.c_int * count)(*ids) - docs_arr = (ctypes.c_char_p * count)() for i, doc in enumerate(documents): docs_arr[i] = _enc(doc) @@ -1201,7 +1238,7 @@ def cactus_index_add(index, ids, documents, metadatas, embeddings): emb_ptrs = (ctypes.POINTER(ctypes.c_float) * count)() emb_arrays = [] - for i, emb in enumerate(embeddings): + for i, emb in enumerate(embeddings or []): arr = (ctypes.c_float * len(emb))(*emb) emb_arrays.append(arr) emb_ptrs[i] = ctypes.cast(arr, ctypes.POINTER(ctypes.c_float)) @@ -1212,21 +1249,23 @@ def cactus_index_add(index, ids, documents, metadatas, embeddings): def cactus_index_delete(index, ids): - """Removes documents from the index by ID.""" - count = len(ids) - ids_arr = (ctypes.c_int * count)(*ids) - rc = _lib.cactus_index_delete(index, ids_arr, count) + """Remove documents from the index by ID.""" + ids_arr = (ctypes.c_int * len(ids))(*ids) + rc = _lib.cactus_index_delete(index, ids_arr, len(ids)) if rc < 0: raise RuntimeError(_err("Failed to delete from index")) -def cactus_index_query(index, embedding, options_json): - """Queries the index by embedding. Returns a JSON string with ids and scores.""" +def cactus_index_query(index, embedding, options=None): + """Query the index by embedding vector. + + Returns: + A dict with "results" — a list of {"id": int, "score": float}. + """ result_capacity = 1000 embedding_dim = len(embedding) emb_arr = (ctypes.c_float * embedding_dim)(*embedding) emb_ptr = ctypes.cast(emb_arr, ctypes.POINTER(ctypes.c_float)) - emb_ptr_ptr = ctypes.pointer(emb_ptr) id_buffer = (ctypes.c_int * result_capacity)() score_buffer = (ctypes.c_float * result_capacity)() id_ptr = ctypes.cast(id_buffer, ctypes.POINTER(ctypes.c_int)) @@ -1234,15 +1273,14 @@ def cactus_index_query(index, embedding, options_json): id_size = ctypes.c_size_t(result_capacity) score_size = ctypes.c_size_t(result_capacity) rc = _lib.cactus_index_query( - index, ctypes.pointer(emb_ptr), 1, embedding_dim, _enc(options_json), + index, ctypes.pointer(emb_ptr), 1, embedding_dim, _to_json(options), ctypes.pointer(id_ptr), ctypes.byref(id_size), - ctypes.pointer(score_ptr), ctypes.byref(score_size) + ctypes.pointer(score_ptr), ctypes.byref(score_size), ) if rc < 0: raise RuntimeError(_err("Index query failed")) n = id_size.value - parts = [f'{{"id":{id_buffer[i]},"score":{score_buffer[i]}}}' for i in range(n)] - return '{"results":[' + ",".join(parts) + ']}' + return {"results": [{"id": int(id_buffer[i]), "score": float(score_buffer[i])} for i in range(n)]} _INDEX_DOC_BUF_SIZE = 4096 @@ -1250,7 +1288,11 @@ def cactus_index_query(index, embedding, options_json): def cactus_index_get(index, ids): - """Retrieves documents by ID. Returns a JSON string with documents, metadata, and embeddings.""" + """Retrieve documents by ID from the index. + + Returns: + A dict with "results" — a list of {"document", "metadata", "embedding"}. + """ count = len(ids) ids_arr = (ctypes.c_int * count)(*ids) doc_raw = [ctypes.create_string_buffer(_INDEX_DOC_BUF_SIZE) for _ in range(count)] @@ -1271,35 +1313,36 @@ def cactus_index_get(index, ids): emb_sizes[i] = _INDEX_EMB_BUF_SIZE rc = _lib.cactus_index_get( index, ids_arr, count, - doc_ptrs, doc_sizes, meta_ptrs, meta_sizes, emb_ptrs, emb_sizes + doc_ptrs, doc_sizes, meta_ptrs, meta_sizes, emb_ptrs, emb_sizes, ) if rc < 0: raise RuntimeError(_err("Failed to get from index")) - parts = [] + results = [] for i in range(count): doc = doc_raw[i].value.decode("utf-8", errors="ignore") meta = meta_raw[i].value.decode("utf-8", errors="ignore") emb = list(emb_raw[i][:emb_sizes[i]]) - meta_str = f'"{meta}"' if meta else "null" - emb_str = "[" + ",".join(str(v) for v in emb) + "]" - parts.append(f'{{"document":"{doc}","metadata":{meta_str},"embedding":{emb_str}}}') - return '{"results":[' + ",".join(parts) + ']}' + results.append({"document": doc, "metadata": meta or None, "embedding": emb}) + return {"results": results} def cactus_index_compact(index): - """Compacts the index storage on disk.""" + """Compact the index storage on disk to reclaim space from deletions.""" rc = _lib.cactus_index_compact(index) if rc < 0: raise RuntimeError(_err("Failed to compact index")) def cactus_index_destroy(index): - """Frees all resources associated with the index handle.""" + """Free all resources associated with an index handle.""" _lib.cactus_index_destroy(index) +# ── Logging ────────────────────────────────────────────────────────── + + def cactus_log_set_level(level): - """Sets the log level: 0=DEBUG, 1=INFO, 2=WARN, 3=ERROR, 4=NONE.""" + """Set the log level: 0=DEBUG, 1=INFO, 2=WARN, 3=ERROR, 4=NONE.""" _lib.cactus_log_set_level(level) @@ -1307,7 +1350,7 @@ def cactus_log_set_level(level): def cactus_log_set_callback(callback): - """Sets a log callback: callback(level, component, message). Pass None to clear.""" + """Set a log callback: callback(level, component, message). Pass None to clear.""" global _log_callback_ref if callback is None: _log_callback_ref = None diff --git a/python/cactus/bindings/graph.py b/python/cactus/bindings/graph.py index 39ecfe9cb..915d54cd8 100644 --- a/python/cactus/bindings/graph.py +++ b/python/cactus/bindings/graph.py @@ -494,11 +494,15 @@ def cat(self, tensors, axis=0): raise ValueError("cat requires at least one tensor") if len(tensors) == 1: return tensors[0] - - result = tensors[0] - for tensor in tensors[1:]: - result = self.concat(result, tensor, axis=axis) - return result + ids = (cactus_node_t * len(tensors))(*(cactus_node_t(t.id) for t in tensors)) + out = cactus_node_t() + rc = _lib.cactus_graph_cat( + self.h, ids, ctypes.c_size_t(len(tensors)), + ctypes.c_int32(int(axis)), ctypes.byref(out), + ) + if rc != 0: + raise RuntimeError("graph_cat failed") + return self._tensor_from_node(out.value) def groupnorm(self, x, weight, bias, num_groups, eps=1e-5): x = self._ensure_tensor(x) @@ -1128,6 +1132,108 @@ def invalidate_persistent(self, persistent_node): if rc != 0: raise RuntimeError("graph_invalidate_persistent failed") + # ── Audio / signal processing ─────────────────────────────────── + + def rfft(self, x): + """Real-to-complex FFT.""" + x = self._ensure_tensor(x) + out = cactus_node_t() + rc = _lib.cactus_graph_rfft(self.h, cactus_node_t(x.id), ctypes.byref(out)) + if rc != 0: + raise RuntimeError("graph_rfft failed") + return self._tensor_from_node(out.value) + + def irfft(self, x, output_length): + """Complex-to-real inverse FFT.""" + x = self._ensure_tensor(x) + out = cactus_node_t() + rc = _lib.cactus_graph_irfft( + self.h, cactus_node_t(x.id), + ctypes.c_size_t(int(output_length)), ctypes.byref(out), + ) + if rc != 0: + raise RuntimeError("graph_irfft failed") + return self._tensor_from_node(out.value) + + def mel_filter_bank(self, num_frequency_bins, num_mel_filters, + min_frequency, max_frequency, sampling_rate, + norm_type=0, scale_type=0): + """Generate a mel-scale filter bank tensor.""" + out = cactus_node_t() + rc = _lib.cactus_graph_mel_filter_bank( + self.h, + ctypes.c_size_t(int(num_frequency_bins)), + ctypes.c_size_t(int(num_mel_filters)), + ctypes.c_float(float(min_frequency)), + ctypes.c_float(float(max_frequency)), + ctypes.c_size_t(int(sampling_rate)), + ctypes.c_int(int(norm_type)), + ctypes.c_int(int(scale_type)), + ctypes.byref(out), + ) + if rc != 0: + raise RuntimeError("graph_mel_filter_bank failed") + return self._tensor_from_node(out.value) + + def spectrogram(self, waveform, mel_filters, frame_length, hop_length, + fft_length, power=2.0, center=True, pad_mode=0, + mel_floor=1e-10, log_mel_mode=0, dither=0.0, + preemphasis=0.0, remove_dc_offset=False): + """Compute a spectrogram from a waveform tensor.""" + waveform = self._ensure_tensor(waveform) + mel_filters = self._ensure_tensor(mel_filters) + out = cactus_node_t() + rc = _lib.cactus_graph_spectrogram( + self.h, + cactus_node_t(waveform.id), + cactus_node_t(mel_filters.id), + ctypes.c_size_t(int(frame_length)), + ctypes.c_size_t(int(hop_length)), + ctypes.c_size_t(int(fft_length)), + ctypes.c_float(float(power)), + ctypes.c_bool(bool(center)), + ctypes.c_int(int(pad_mode)), + ctypes.c_float(float(mel_floor)), + ctypes.c_int(int(log_mel_mode)), + ctypes.c_float(float(dither)), + ctypes.c_float(float(preemphasis)), + ctypes.c_bool(bool(remove_dc_offset)), + ctypes.byref(out), + ) + if rc != 0: + raise RuntimeError("graph_spectrogram failed") + return self._tensor_from_node(out.value) + + # ── Image processing ───────────────────────────────────────────── + + def image_preprocess(self, pixel_input, src_width, src_height, + target_width, target_height, patch_size, channels, + rescale_factor, mean, std_dev): + """Resize, normalize, and patch an image tensor. + + *mean* and *std_dev* are per-channel float sequences (length = channels). + """ + pixel_input = self._ensure_tensor(pixel_input) + mean_arr = (ctypes.c_float * len(mean))(*[float(v) for v in mean]) + std_arr = (ctypes.c_float * len(std_dev))(*[float(v) for v in std_dev]) + out = cactus_node_t() + rc = _lib.cactus_graph_image_preprocess( + self.h, cactus_node_t(pixel_input.id), + ctypes.c_int(int(src_width)), + ctypes.c_int(int(src_height)), + ctypes.c_int(int(target_width)), + ctypes.c_int(int(target_height)), + ctypes.c_int(int(patch_size)), + ctypes.c_int(int(channels)), + ctypes.c_float(float(rescale_factor)), + mean_arr, std_arr, ctypes.byref(out), + ) + if rc != 0: + raise RuntimeError("graph_image_preprocess failed") + return self._tensor_from_node(out.value) + + # ── Introspection ──────────────────────────────────────────────── + def output_info(self, x): x = self._ensure_tensor(x) return self._get_output_info(x.id) diff --git a/python/cactus/cli/__init__.py b/python/cactus/cli/__init__.py index 14fc7b1b8..ec63872bf 100644 --- a/python/cactus/cli/__init__.py +++ b/python/cactus/cli/__init__.py @@ -4,15 +4,45 @@ from .common import ( DEFAULT_MODEL_ID, DEFAULT_TEST_MODEL_ID, + DEFAULT_ASR_MODEL_ID, ) from .download import cmd_download from .compile import cmd_build from .run import cmd_run -from .transcribe import cmd_transcribe, DEFAULT_ASR_MODEL_ID +from .transcribe import cmd_transcribe from .test import cmd_test from .convert import cmd_convert from .eval import cmd_eval -from .misc import cmd_auth, cmd_clean +from .auth import cmd_auth +from .clean import cmd_clean + + +# ── Shared parent parsers ───────────────────────────────────────────── + + +def _model_parent(): + """Args shared by commands that take a model ID.""" + p = argparse.ArgumentParser(add_help=False) + p.add_argument("model_id", nargs="?", default=DEFAULT_MODEL_ID, + help=f"HuggingFace model ID (default: {DEFAULT_MODEL_ID})") + p.add_argument("--bits", type=int, choices=[1, 2, 3, 4], default=4, + help="CQ quantization bits (default: 4)") + p.add_argument("--cache-dir", help="Cache directory for HuggingFace models") + p.add_argument("--token", help="HuggingFace API token") + p.add_argument("--reconvert", action="store_true", + help="Force conversion from source") + return p + + +def _telemetry_parent(): + """Args shared by commands that support telemetry toggle.""" + p = argparse.ArgumentParser(add_help=False) + p.add_argument("--no-cloud-tele", action="store_true", + help="Disable cloud telemetry (write to cache only)") + return p + + +# ── Parser setup ────────────────────────────────────────────────────── def create_parser(): @@ -62,22 +92,21 @@ def create_parser(): ----------------------------------------------------------------- - cactus download downloads CQ model files - auto-resolves Cactus-Compute CQ repo + cactus download downloads model from HuggingFace Optional flags: - --bits 1|2|3|4 CQ quantization bits (default: 4) --token HuggingFace API token - --reconvert force FP16 conversion from source ----------------------------------------------------------------- cactus convert [output_dir] converts HuggingFace model to CQ format - and transpiles the graph into the same folder + downloads pre-converted from Cactus-Compute + if available, otherwise converts locally Optional flags: --bits 1|2|3|4 CQ quantization bits (default: 4) --token HuggingFace API token + --reconvert force local conversion ----------------------------------------------------------------- @@ -104,16 +133,7 @@ def create_parser(): --whisper_model default: openai/whisper-small (language detection) --benchmark use larger models (LFM2.5-VL-1.6B + nvidia/parakeet-ctc-1.1b) --reconvert force model weights reconversion from source - --llm run only LLM tests - --vlm run only VLM tests - --stt run only speech-to-text tests - --embed run only embedding tests - --rag run only RAG tests - --graph run only graph tests - --index run only index tests - --kernel run only kernel tests - --kv_cache run only KV cache tests - --performance run only performance benchmarks + --suite run a specific test suite (auto-detected) --ios run on connected iPhone --android run on connected Android @@ -151,152 +171,139 @@ def create_parser(): parser._action_groups = [] - download_parser = subparsers.add_parser('download', help='Download CQ model files') - download_parser.add_argument('model_id', nargs='?', default=DEFAULT_MODEL_ID, - help=f'HuggingFace model ID or Cactus-Compute CQ repo (default: {DEFAULT_MODEL_ID})') - download_parser.add_argument('--bits', type=int, choices=[1, 2, 3, 4], default=4, - help='CQ quantization bits (default: 4)') - download_parser.add_argument('--cache-dir', help='Cache directory for HuggingFace models') - download_parser.add_argument('--token', help='HuggingFace API token') - download_parser.add_argument('--reconvert', action='store_true', - help='Force FP16 conversion from source (skip CQ download)') - - build_parser = subparsers.add_parser('build', help='Build the chat application') - build_parser.add_argument('--apple', action='store_true', - help='Build for Apple platforms (iOS/macOS)') - build_parser.add_argument('--android', action='store_true', - help='Build for Android') - build_parser.add_argument('--python', action='store_true', - help='Build shared library for Python FFI') - - run_parser = subparsers.add_parser('run', help='Build, download (if needed), and run chat') - run_parser.add_argument('model_id', nargs='?', default=DEFAULT_MODEL_ID, - help=f'HuggingFace model ID (default: {DEFAULT_MODEL_ID})') - run_parser.add_argument('--cache-dir', help='Cache directory for HuggingFace models') - run_parser.add_argument('--token', help='HuggingFace API token') - run_parser.add_argument('--no-cloud-tele', action='store_true', - help='Disable cloud telemetry (write to cache only)') - run_parser.add_argument('--reconvert', action='store_true', - help='Download original model and convert (instead of using pre-converted from Cactus-Compute)') - run_parser.add_argument('--image', - help='Path to image file for VLM inference (attached to first message)') - run_parser.add_argument('--audio', - help='Path to audio file (WAV) for audio chat (attached to first message)') - run_parser.add_argument('--file', dest='audio_file', default=None, - help='Audio file for transpiled bundles when model_id points to a transpiled folder') - run_parser.add_argument('--image-file', action='append', default=[], - help='Repeatable image input for transpiled bundles') - run_parser.add_argument('--weights-dir', default=None, - help='Converted weights directory for transpiled bundles with bound weights') - run_parser.add_argument('--system', - help='System prompt to prepend to all messages') - run_parser.add_argument('--prompt', - help='Initial prompt to send immediately') - run_parser.add_argument('--input-ids', default=None, - help='Comma-separated token ids for transpiled causal-LM bundles') - run_parser.add_argument('--max-new-tokens', type=int, default=None, - help='Maximum tokens to generate for transpiled causal-LM bundles') - run_parser.add_argument('--stop-sequence', action='append', default=[], - help='Optional stop sequence for transpiled causal-LM bundles. Repeatable.') - run_parser.add_argument('--result-json', default=None, - help='Optional path to save transpiled bundle results as JSON') - run_parser.add_argument('--thinking', action='store_true', - help='Enable thinking/reasoning for models that support it') - - transcribe_parser = subparsers.add_parser('transcribe', help='Download ASR model and run transcription') - transcribe_parser.add_argument('model_id', nargs='?', default=DEFAULT_ASR_MODEL_ID, - help=f'HuggingFace model ID (default: {DEFAULT_ASR_MODEL_ID})') - transcribe_parser.add_argument('--file', dest='audio_file', default=None, - help='Audio file to transcribe (WAV format). Omit for live microphone.') - transcribe_parser.add_argument('--language', default='en', - help='Language code for transcription (default: en). Examples: es, fr, de, zh, ja') - transcribe_parser.add_argument('--cache-dir', help='Cache directory for HuggingFace models') - transcribe_parser.add_argument('--token', help='HuggingFace API token') - transcribe_parser.add_argument('--no-cloud-tele', action='store_true', - help='Disable cloud telemetry (write to cache only)') - transcribe_parser.add_argument('--force-handoff', action='store_true', - help='Force cloud handoff by assuming low confidence') - transcribe_parser.add_argument('--reconvert', action='store_true', - help='Download original model and convert (instead of using pre-converted from Cactus-Compute)') - transcribe_parser.add_argument('--android', action='store_true', - help='Run transcription on a connected Android device (requires --file)') - transcribe_parser.add_argument('--ios', action='store_true', - help='Run transcription on a connected iOS device (requires --file)') - transcribe_parser.add_argument('--device', default=None, - help='ADB device ID to use with --android') - - eval_parser = subparsers.add_parser('eval', help='Run evaluation scripts outside the cactus submodule') - eval_parser.add_argument('model_id', nargs='?', default=DEFAULT_MODEL_ID, - help=f'HuggingFace model ID (default: {DEFAULT_MODEL_ID})') - eval_parser.add_argument('--cache-dir', help='Cache directory for HuggingFace models') - eval_parser.add_argument('--token', help='HuggingFace API token') - eval_parser.add_argument('--tools', action='store_true', help='Run tools evals (default)') - eval_parser.add_argument('--vlm', action='store_true', help='Run VLM-specific evals') - eval_parser.add_argument('--stt', action='store_true', help='Run speech-to-text evals') - eval_parser.add_argument('--llm', action='store_true', help='Run LLM evals') - eval_parser.add_argument('--embed', action='store_true', help='Run embedding evals') - eval_parser.add_argument('--no-cloud-tele', action='store_true', - help='Disable cloud telemetry (write to cache only)') - eval_parser.add_argument('--reconvert', action='store_true', - help='Download original model and convert (instead of using pre-converted from Cactus-Compute)') - - test_parser = subparsers.add_parser('test', help='Run the test suite') - test_parser.add_argument('--model', default=DEFAULT_TEST_MODEL_ID, - help='Model to use for tests (default: Gemma4)') - test_parser.add_argument('--token', help='HuggingFace API token') - test_parser.add_argument('--android', action='store_true', - help='Run tests on Android') - test_parser.add_argument('--ios', action='store_true', - help='Run tests on iOS') - test_parser.add_argument('--only', help='(deprecated, use -- instead) Only run the specified test') - for _test_name in ['llm', 'vlm', 'stt', 'embed', 'rag', 'graph', 'index', 'kernel', 'kv_cache', 'performance']: - test_parser.add_argument(f'--{_test_name}', action='store_true', - help=f'Only run the {_test_name} tests') - test_parser.add_argument('--enable-telemetry', action='store_true', - help='Enable cloud telemetry (disabled by default in tests)') - test_parser.add_argument('--reconvert', action='store_true', - help='Download original model and convert (instead of using pre-converted from Cactus-Compute)') - - auth_parser = subparsers.add_parser('auth', help='Manage Cactus Cloud API key') - auth_parser.add_argument('--clear', action='store_true', - help='Remove the saved API key') - auth_parser.add_argument('--status', action='store_true', - help='Show current key status without prompting') - - clean_parser = subparsers.add_parser('clean', help='Remove all build artifacts') - - - convert_parser = subparsers.add_parser('convert', help='Convert HuggingFace model to CQ format') - convert_parser.add_argument('model_name', help='HuggingFace model name') - convert_parser.add_argument('output_dir', nargs='?', default=None, - help='Output directory (default: weights/)') - convert_parser.add_argument('--bits', type=int, choices=[1, 2, 3, 4], default=4, - help='CQ quantization bits (default: 4)') - convert_parser.add_argument('--cache-dir', help='Cache directory for HuggingFace models') - convert_parser.add_argument('--token', help='HuggingFace API token') - convert_parser.add_argument('--task', default='auto', - choices=['auto', 'causal_lm_logits', 'multimodal_causal_lm_logits', - 'ctc_logits', 'encoder_hidden_states', - 'seq2seq_transcription', 'tdt_transcription'], - help='Transpile task after conversion (default: auto)') - convert_parser.add_argument('--prompt', - help='Prompt used for causal or multimodal graph shape capture') - convert_parser.add_argument('--system-prompt', default='', - help='Optional system prompt for multimodal prompt construction') - convert_parser.add_argument('--image-file', action='append', default=[], - help='Representative image file for multimodal transpile') - convert_parser.add_argument('--audio-file', - help='Representative audio file for audio/multimodal transpile') - convert_parser.add_argument('--max-new-tokens', type=int, default=None, - help='Generation room to preallocate for causal decode graphs') - convert_parser.add_argument('--component-pipeline', default='auto', choices=['auto', 'on', 'off'], - help='Use split component graph transpilation when supported') - convert_parser.add_argument('--components', - help='Comma-separated component subset for component-pipeline models') - convert_parser.add_argument('--trust-remote-code', action='store_true', - help='Allow HF remote code during the transpile phase') - convert_parser.add_argument('--local-files-only', action='store_true', - help='Require HF model/processor files to already be local during transpile') + # ── download ────────────────────────────────────────────────────── + download_parser = subparsers.add_parser("download", help="Download model from HuggingFace") + download_parser.add_argument("model_id", nargs="?", default=DEFAULT_MODEL_ID, + help=f"HuggingFace model ID (default: {DEFAULT_MODEL_ID})") + download_parser.add_argument("--cache-dir", help="Cache directory for HuggingFace models") + download_parser.add_argument("--token", help="HuggingFace API token") + + # ── build ───────────────────────────────────────────────────────── + build_parser = subparsers.add_parser("build", help="Build the chat application") + build_parser.add_argument("--apple", action="store_true", + help="Build for Apple platforms (iOS/macOS)") + build_parser.add_argument("--android", action="store_true", + help="Build for Android") + build_parser.add_argument("--python", action="store_true", + help="Build shared library for Python FFI") + + # ── run ─────────────────────────────────────────────────────────── + run_parser = subparsers.add_parser("run", help="Build, download (if needed), and run chat", + parents=[_model_parent(), _telemetry_parent()]) + run_parser.add_argument("--image", + help="Path to image file for VLM inference (attached to first message)") + run_parser.add_argument("--audio", + help="Path to audio file (WAV) for audio chat (attached to first message)") + run_parser.add_argument("--file", dest="audio_file", default=None, + help="Audio file for transpiled bundles when model_id points to a transpiled folder") + run_parser.add_argument("--image-file", action="append", default=[], + help="Repeatable image input for transpiled bundles") + run_parser.add_argument("--weights-dir", default=None, + help="Converted weights directory for transpiled bundles with bound weights") + run_parser.add_argument("--system", + help="System prompt to prepend to all messages") + run_parser.add_argument("--prompt", + help="Initial prompt to send immediately") + run_parser.add_argument("--input-ids", default=None, + help="Comma-separated token ids for transpiled causal-LM bundles") + run_parser.add_argument("--max-new-tokens", type=int, default=None, + help="Maximum tokens to generate for transpiled causal-LM bundles") + run_parser.add_argument("--stop-sequence", action="append", default=[], + help="Optional stop sequence for transpiled causal-LM bundles. Repeatable.") + run_parser.add_argument("--result-json", default=None, + help="Optional path to save transpiled bundle results as JSON") + run_parser.add_argument("--thinking", action="store_true", + help="Enable thinking/reasoning for models that support it") + + # ── transcribe ──────────────────────────────────────────────────── + transcribe_parser = subparsers.add_parser("transcribe", help="Download ASR model and run transcription", + parents=[_telemetry_parent()]) + transcribe_parser.add_argument("model_id", nargs="?", default=DEFAULT_ASR_MODEL_ID, + help=f"HuggingFace model ID (default: {DEFAULT_ASR_MODEL_ID})") + transcribe_parser.add_argument("--file", dest="audio_file", default=None, + help="Audio file to transcribe (WAV format). Omit for live microphone.") + transcribe_parser.add_argument("--language", default="en", + help="Language code for transcription (default: en). Examples: es, fr, de, zh, ja") + transcribe_parser.add_argument("--cache-dir", help="Cache directory for HuggingFace models") + transcribe_parser.add_argument("--token", help="HuggingFace API token") + transcribe_parser.add_argument("--force-handoff", action="store_true", + help="Force cloud handoff by assuming low confidence") + transcribe_parser.add_argument("--reconvert", action="store_true", + help="Download original model and convert (instead of using pre-converted from Cactus-Compute)") + transcribe_parser.add_argument("--android", action="store_true", + help="Run transcription on a connected Android device (requires --file)") + transcribe_parser.add_argument("--ios", action="store_true", + help="Run transcription on a connected iOS device (requires --file)") + transcribe_parser.add_argument("--device", default=None, + help="ADB device ID to use with --android") + + # ── eval ────────────────────────────────────────────────────────── + eval_parser = subparsers.add_parser("eval", help="Run evaluation scripts outside the cactus submodule", + parents=[_model_parent(), _telemetry_parent()]) + eval_parser.add_argument("--suite", choices=["tools", "llm", "vlm", "stt"], + default="tools", help="Eval suite to run (default: tools)") + + # ── test ────────────────────────────────────────────────────────── + test_parser = subparsers.add_parser("test", help="Run the test suite") + test_parser.add_argument("--model", default=DEFAULT_TEST_MODEL_ID, + help="Model to use for tests (default: Gemma4)") + test_parser.add_argument("--token", help="HuggingFace API token") + test_parser.add_argument("--android", action="store_true", + help="Run tests on Android") + test_parser.add_argument("--ios", action="store_true", + help="Run tests on iOS") + from .test import discover_suites + test_parser.add_argument("--suite", choices=discover_suites(), + help="Run a specific test suite") + test_parser.add_argument("--enable-telemetry", action="store_true", + help="Enable cloud telemetry (disabled by default in tests)") + test_parser.add_argument("--reconvert", action="store_true", + help="Download original model and convert (instead of using pre-converted from Cactus-Compute)") + + # ── auth ────────────────────────────────────────────────────────── + auth_parser = subparsers.add_parser("auth", help="Manage Cactus Cloud API key") + auth_parser.add_argument("--clear", action="store_true", + help="Remove the saved API key") + auth_parser.add_argument("--status", action="store_true", + help="Show current key status without prompting") + + # ── clean ───────────────────────────────────────────────────────── + subparsers.add_parser("clean", help="Remove all build artifacts") + + # ── convert ─────────────────────────────────────────────────────── + convert_parser = subparsers.add_parser("convert", help="Convert HuggingFace model to CQ format") + convert_parser.add_argument("model_name", help="HuggingFace model name") + convert_parser.add_argument("output_dir", nargs="?", default=None, + help="Output directory (default: weights/)") + convert_parser.add_argument("--bits", type=int, choices=[1, 2, 3, 4], default=4, + help="CQ quantization bits (default: 4)") + convert_parser.add_argument("--cache-dir", help="Cache directory for HuggingFace models") + convert_parser.add_argument("--token", help="HuggingFace API token") + convert_parser.add_argument("--task", default="auto", + choices=["auto", "causal_lm_logits", "multimodal_causal_lm_logits", + "ctc_logits", "encoder_hidden_states", + "seq2seq_transcription", "tdt_transcription"], + help="Transpile task after conversion (default: auto)") + convert_parser.add_argument("--prompt", + help="Prompt used for causal or multimodal graph shape capture") + convert_parser.add_argument("--system-prompt", default="", + help="Optional system prompt for multimodal prompt construction") + convert_parser.add_argument("--image-file", action="append", default=[], + help="Representative image file for multimodal transpile") + convert_parser.add_argument("--audio-file", + help="Representative audio file for audio/multimodal transpile") + convert_parser.add_argument("--max-new-tokens", type=int, default=None, + help="Generation room to preallocate for causal decode graphs") + convert_parser.add_argument("--component-pipeline", default="auto", choices=["auto", "on", "off"], + help="Use split component graph transpilation when supported") + convert_parser.add_argument("--components", + help="Comma-separated component subset for component-pipeline models") + convert_parser.add_argument("--trust-remote-code", action="store_true", + help="Allow HF remote code during the transpile phase") + convert_parser.add_argument("--local-files-only", action="store_true", + help="Require HF model/processor files to already be local during transpile") + convert_parser.add_argument("--reconvert", action="store_true", + help="Force conversion from source") return parser @@ -314,6 +321,21 @@ def preprocess_eval_args(parser, argv): return args +# ── Command dispatch ────────────────────────────────────────────────── + +_COMMANDS = { + "download": cmd_download, + "build": cmd_build, + "run": cmd_run, + "transcribe": cmd_transcribe, + "test": cmd_test, + "eval": cmd_eval, + "auth": cmd_auth, + "clean": cmd_clean, + "convert": cmd_convert, +} + + def main(): """Main entry point for the Cactus CLI.""" parser = create_parser() @@ -321,24 +343,9 @@ def main(): argv = sys.argv[1:] args = preprocess_eval_args(parser, argv) - if args.command == 'download': - sys.exit(cmd_download(args)) - elif args.command == 'build': - sys.exit(cmd_build(args)) - elif args.command == 'run': - sys.exit(cmd_run(args)) - elif args.command == 'transcribe': - sys.exit(cmd_transcribe(args)) - elif args.command == 'test': - sys.exit(cmd_test(args)) - elif args.command == 'eval': - sys.exit(cmd_eval(args)) - elif args.command == 'auth': - sys.exit(cmd_auth(args)) - elif args.command == 'clean': - sys.exit(cmd_clean(args)) - elif args.command == 'convert': - sys.exit(cmd_convert(args)) + handler = _COMMANDS.get(args.command) + if handler: + sys.exit(handler(args)) else: parser.print_help() sys.exit(1) diff --git a/python/cactus/cli/auth.py b/python/cactus/cli/auth.py new file mode 100644 index 000000000..ae76537c4 --- /dev/null +++ b/python/cactus/cli/auth.py @@ -0,0 +1,33 @@ +from .common import print_color, GREEN + + +def cmd_auth(args): + """Manage Cactus Cloud API key.""" + from .config_utils import CactusConfig + + config = CactusConfig() + + if args.clear: + config.clear_api_key() + print_color(GREEN, "API key cleared.") + return 0 + + api_key = config.get_api_key() + + if api_key: + masked = api_key[:4] + "..." + api_key[-4:] + print(f"Current API key: {masked}") + else: + print("No API key set.") + + if args.status: + return 0 + + print() + print("Get your cloud key at \033[1;36mhttps://www.cactuscompute.com/dashboard/api-keys\033[0m") + new_key = input("Enter new API key (press Enter to skip): ").strip() + if new_key: + config.set_api_key(new_key) + masked = new_key[:4] + "..." + new_key[-4:] + print_color(GREEN, f"API key saved: {masked}") + return 0 diff --git a/python/cactus/cli/misc.py b/python/cactus/cli/clean.py similarity index 79% rename from python/cactus/cli/misc.py rename to python/cactus/cli/clean.py index 7dd9377be..88fec09df 100644 --- a/python/cactus/cli/misc.py +++ b/python/cactus/cli/clean.py @@ -4,44 +4,11 @@ from .common import ( PROJECT_ROOT, - get_weights_dir, print_color, - RED, GREEN, YELLOW, BLUE, NC, + GREEN, YELLOW, BLUE, ) -def cmd_auth(args): - """Manage Cactus Cloud API key.""" - from .config_utils import CactusConfig - - config = CactusConfig() - - if args.clear: - config.clear_api_key() - print_color(GREEN, "API key cleared.") - return 0 - - api_key = config.get_api_key() - - if api_key: - masked = api_key[:4] + "..." + api_key[-4:] - print(f"Current API key: {masked}") - else: - print("No API key set.") - - if args.status: - return 0 - - print() - print("Get your cloud key at \033[1;36mhttps://www.cactuscompute.com/dashboard/api-keys\033[0m") - new_key = input("Enter new API key (press Enter to skip): ").strip() - if new_key: - config.set_api_key(new_key) - masked = new_key[:4] + "..." + new_key[-4:] - print_color(GREEN, f"API key saved: {masked}") - return 0 - - def cmd_clean(args): """Remove all build artifacts, caches, and downloaded weights.""" print_color(BLUE, "Cleaning all build artifacts from Cactus project...") @@ -69,7 +36,6 @@ def remove_if_exists(path): remove_if_exists(PROJECT_ROOT / "weights") - # Clean telemetry cache telemetry_cache = Path.home() / "Library" / "Caches" / "cactus" / "telemetry" if telemetry_cache.exists(): print(f"Removing telemetry cache: {telemetry_cache}") @@ -77,7 +43,6 @@ def remove_if_exists(path): else: print(f"Telemetry cache not found: {telemetry_cache}") - # Re-cache API key from config so users don't need to run `cactus auth` again from .config_utils import CactusConfig config = CactusConfig() saved_key = config.load_config().get("api_key", "") @@ -95,7 +60,7 @@ def remove_if_exists(path): PROJECT_ROOT / "libs" / "mbedtls", ] - def should_preserve_artifact(path: Path) -> bool: + def should_preserve_artifact(path): try: resolved = path.resolve() except FileNotFoundError: @@ -159,7 +124,6 @@ def should_preserve_artifact(path: Path) -> bool: print("All build artifacts have been removed.") print() - # Re-run setup automatically print_color(BLUE, "Re-running setup...") setup_script = PROJECT_ROOT / "setup" result = subprocess.run( diff --git a/python/cactus/cli/common.py b/python/cactus/cli/common.py index 6b0a3453b..c6fd74b2f 100644 --- a/python/cactus/cli/common.py +++ b/python/cactus/cli/common.py @@ -3,20 +3,19 @@ import os import subprocess import shutil -import platform from pathlib import Path SCRIPT_DIR = Path(__file__).resolve().parent -def _looks_like_project_root(path: Path) -> bool: +def _looks_like_project_root(path): return ( (path / "python" / "cactus" / "cli").is_dir() and (path / "cactus-kernels").is_dir() ) -def _resolve_project_root() -> Path: +def _resolve_project_root(): env_root = os.getenv("CACTUS_PROJECT_ROOT", "").strip() if env_root: candidate = Path(env_root).expanduser().resolve() @@ -32,13 +31,13 @@ def _resolve_project_root() -> Path: if _looks_like_project_root(candidate): return candidate - # Final fallback for unusual layouts. return module_root PROJECT_ROOT = _resolve_project_root() DEFAULT_MODEL_ID = "google/gemma-4-E2B-it" DEFAULT_TEST_MODEL_ID = "google/gemma-4-E2B-it" +DEFAULT_ASR_MODEL_ID = "nvidia/parakeet-tdt-0.6b-v3" RED = '\033[0;31m' @@ -53,36 +52,13 @@ def print_color(color, message): print(f"{color}{message}{NC}") -from .download import ( - combo_label, - download_cq_archive, - get_model_dir_name, - get_weights_dir, - list_hf_cq_archives, - resolve_archive, - suggested_cq_repo, -) - - -def get_effective_weights_dir(model_id, args=None): - return get_weights_dir(model_id) - - - def check_command(cmd): """Check if a command is available in PATH.""" return shutil.which(cmd) is not None def run_command(cmd, cwd=None, check=True): - """Run a script or command and optionally exit on failure. - - Args: - cmd: Script path (str) or command list. String paths are executed - directly without shell interpretation to handle spaces safely. - cwd: Working directory for the command. - check: If True, exit on non-zero return code. - """ + """Run a script or command and optionally exit on failure.""" if isinstance(cmd, str): cmd = [cmd] result = subprocess.run(cmd, cwd=cwd) @@ -92,7 +68,7 @@ def run_command(cmd, cwd=None, check=True): def prompt_for_api_key(config): - """Prompt user to set Cactus Cloud API key if not already configured. Returns the key or empty string.""" + """Prompt user to set Cactus Cloud API key if not already configured.""" api_key = config.get_api_key() if api_key: return api_key diff --git a/python/cactus/cli/compile.py b/python/cactus/cli/compile.py index 9681600ab..55dcc30b0 100644 --- a/python/cactus/cli/compile.py +++ b/python/cactus/cli/compile.py @@ -1,4 +1,3 @@ -import os import subprocess import platform from pathlib import Path @@ -14,8 +13,6 @@ def check_libcurl(): """Check if libcurl development libraries are installed.""" - import platform - if platform.system() == 'Darwin': return True @@ -37,101 +34,81 @@ def check_libcurl(): return False -def cmd_build(args): - """Build the Cactus library.""" - if getattr(args, 'apple', False): - return cmd_build_apple(args) - if getattr(args, 'android', False): - return cmd_build_android(args) - if getattr(args, 'flutter', False): - return cmd_build_flutter(args) - if getattr(args, 'python', False): - return cmd_build_python(args) - - print_color(BLUE, "Building Cactus library...") - print("=" * 24) - - if not check_command('cmake'): - print_color(RED, "Error: CMake is not installed") - print(" macOS: brew install cmake") - print(" Ubuntu: sudo apt-get install cmake build-essential") - return 1 - - if not check_libcurl(): - print_color(RED, "Error: libcurl development libraries not found") - print(" macOS: brew install curl") - print(" Ubuntu: sudo apt-get install libcurl4-openssl-dev") - return 1 - - cactus_dir = PROJECT_ROOT / "cactus" - lib_path = cactus_dir / "build" / "libcactus.a" - - print_color(YELLOW, "Building Cactus library...") - build_script = cactus_dir / "build.sh" - if not build_script.exists(): - print_color(RED, f"Error: build.sh not found at {build_script}") - return 1 - result = run_command(str(build_script), cwd=cactus_dir, check=False) - if result.returncode != 0: - print_color(RED, "Failed to build cactus library") - return 1 - tests_dir = PROJECT_ROOT / "cactus-engine" / "tests" - build_dir = tests_dir / "build" - build_dir.mkdir(parents=True, exist_ok=True) - - print("Compiling chat.cpp...") - - chat_cpp = tests_dir / "chat.cpp" - if not chat_cpp.exists(): - print_color(RED, f"Error: chat.cpp not found at {chat_cpp}") - return 1 - +def _detect_sdl2() -> tuple[list[str], list[str]]: + """Detect SDL2 and return (compiler_flags, linker_flags).""" is_darwin = platform.system() == "Darwin" - vendored_curl = PROJECT_ROOT / "cactus-engine" / "libs" / "curl" / "lib" / "libcurl.a" - sdl2_available = False - sdl2_flags = [] - sdl2_link = [] if is_darwin: sdl2_check = subprocess.run(["brew", "list", "sdl2"], capture_output=True) if sdl2_check.returncode == 0: - sdl2_prefix_result = subprocess.run(["brew", "--prefix", "sdl2"], capture_output=True, text=True) + sdl2_prefix_result = subprocess.run( + ["brew", "--prefix", "sdl2"], capture_output=True, text=True, + ) if sdl2_prefix_result.returncode == 0: sdl2_prefix = sdl2_prefix_result.stdout.strip() - sdl2_flags = ["-DHAVE_SDL2", f"-I{sdl2_prefix}/include", f"-I{sdl2_prefix}/include/SDL2"] - sdl2_link = [f"-L{sdl2_prefix}/lib", "-lSDL2"] - sdl2_available = True + return ( + ["-DHAVE_SDL2", f"-I{sdl2_prefix}/include", f"-I{sdl2_prefix}/include/SDL2"], + [f"-L{sdl2_prefix}/lib", "-lSDL2"], + ) else: sdl2_check = subprocess.run(["pkg-config", "--exists", "sdl2"], capture_output=True) if sdl2_check.returncode == 0: cflags = subprocess.run(["pkg-config", "--cflags", "sdl2"], capture_output=True, text=True) libs = subprocess.run(["pkg-config", "--libs", "sdl2"], capture_output=True, text=True) if cflags.returncode == 0 and libs.returncode == 0: - sdl2_flags = ["-DHAVE_SDL2"] + cflags.stdout.strip().split() - sdl2_link = libs.stdout.strip().split() - sdl2_available = True + return ( + ["-DHAVE_SDL2"] + cflags.stdout.strip().split(), + libs.stdout.strip().split(), + ) + + return [], [] + + +def build_binary( + name: str, + lib_path: Path, + *, + sdl2: tuple[list[str], list[str]] | None = None, +) -> int: + """Compile a single C++ binary (chat or asr) against libcactus.a. + + Handles darwin vs linux, vendored curl. + Pass *sdl2=(flags, link)* to reuse a prior detection; None re-detects. + Returns 0 on success. + """ + tests_dir = PROJECT_ROOT / "cactus-engine" / "tests" + source = tests_dir / f"{name}.cpp" + build_dir = tests_dir / "build" + build_dir.mkdir(parents=True, exist_ok=True) - if sdl2_available: - print_color(GREEN, "SDL2 found - building with live audio support") - else: - print_color(YELLOW, "SDL2 not found - live mic recording will be disabled") - print_color(YELLOW, "Install SDL2 for live mic support: brew install sdl2 (macOS)") - print_color(YELLOW, "Then run `cactus build`") + if not source.exists(): + print_color(RED, f"Error: {name}.cpp not found at {source}") + return 1 + + is_darwin = platform.system() == "Darwin" + sdl2_flags, sdl2_link = sdl2 if sdl2 is not None else _detect_sdl2() + + include_dirs = [PROJECT_ROOT] + if name == "chat": + include_dirs += [ + PROJECT_ROOT / "cactus-engine", + PROJECT_ROOT / "cactus-graph", + PROJECT_ROOT / "cactus-kernels", + ] + + print(f"Compiling {name}.cpp...") if is_darwin: + vendored_curl = PROJECT_ROOT / "cactus-engine" / "libs" / "curl" / "lib" / "libcurl.a" curl_link = [str(vendored_curl)] if vendored_curl.exists() else ["-lcurl"] compiler = "clang++" cmd = [ compiler, "-std=c++20", "-O3", "-DACCELERATE_NEW_LAPACK", - f"-I{PROJECT_ROOT}", - f"-I{PROJECT_ROOT / 'cactus-engine'}", - f"-I{PROJECT_ROOT / 'cactus-graph'}", - f"-I{PROJECT_ROOT / 'cactus-kernels'}", + *[f"-I{d}" for d in include_dirs], *sdl2_flags, - str(chat_cpp), - str(lib_path), - "-o", "chat", + str(source), str(lib_path), + "-o", name, *curl_link, "-framework", "Accelerate", "-framework", "CoreML", @@ -145,16 +122,11 @@ def cmd_build(args): compiler = "g++" cmd = [ compiler, "-std=c++20", "-O3", - f"-I{PROJECT_ROOT}", - f"-I{PROJECT_ROOT / 'cactus-engine'}", - f"-I{PROJECT_ROOT / 'cactus-graph'}", - f"-I{PROJECT_ROOT / 'cactus-kernels'}", + *[f"-I{d}" for d in include_dirs], *sdl2_flags, - str(chat_cpp), - str(lib_path), - "-o", "chat", - "-lcurl", - "-pthread", + str(source), str(lib_path), + "-o", name, + "-lcurl", "-pthread", *sdl2_link, ] @@ -164,130 +136,98 @@ def cmd_build(args): result = subprocess.run(cmd, cwd=build_dir) if result.returncode != 0: - print_color(RED, "Build failed") + print_color(RED, f"{name} build failed") return 1 - print_color(GREEN, f"Build complete: {build_dir / 'chat'}") - - asr_cpp = tests_dir / "asr.cpp" - if asr_cpp.exists(): - print("Compiling asr.cpp...") - - if is_darwin: - curl_link = [str(vendored_curl)] if vendored_curl.exists() else ["-lcurl"] - cmd = [ - compiler, "-std=c++20", "-O3", - "-DACCELERATE_NEW_LAPACK", - f"-I{PROJECT_ROOT}", - *sdl2_flags, - str(asr_cpp), - str(lib_path), - "-o", "asr", - *curl_link, - "-framework", "Accelerate", - "-framework", "CoreML", - "-framework", "Foundation", - "-framework", "Security", - "-framework", "SystemConfiguration", - "-framework", "CFNetwork", - *sdl2_link, - ] - else: - cmd = [ - compiler, "-std=c++20", "-O3", - f"-I{PROJECT_ROOT}", - *sdl2_flags, - str(asr_cpp), - str(lib_path), - "-o", "asr", - "-lcurl", - "-pthread", - *sdl2_link, - ] - - result = subprocess.run(cmd, cwd=build_dir) - if result.returncode != 0: - print_color(RED, "ASR build failed") - return 1 - - print_color(GREEN, f"Build complete: {build_dir / 'asr'}") + print_color(GREEN, f"Build complete: {build_dir / name}") + return 0 - print_color(GREEN, "Cactus library built successfully!") - print(f"Library location: {lib_path}") - return 0 +def cmd_build(args): + """Build the Cactus library.""" + if args.apple: + return _build_with_script("apple", "Building Cactus for Apple platforms") + if args.android: + return _build_with_script("android", "Building Cactus for Android") + if args.python: + return cmd_build_python() + print_color(BLUE, "Building Cactus library...") + print("=" * 24) -def cmd_build_apple(args): - """Build Cactus for Apple platforms (iOS/macOS).""" - print_color(BLUE, "Building Cactus for Apple platforms...") - print("=" * 40) + if not check_command('cmake'): + print_color(RED, "Error: CMake is not installed") + print(" macOS: brew install cmake") + print(" Ubuntu: sudo apt-get install cmake build-essential") + return 1 - if platform.system() != "Darwin": - print_color(RED, "Error: Apple builds require macOS") + if not check_libcurl(): + print_color(RED, "Error: libcurl development libraries not found") + print(" macOS: brew install curl") + print(" Ubuntu: sudo apt-get install libcurl4-openssl-dev") return 1 - build_script = PROJECT_ROOT / "apple" / "build.sh" + cactus_dir = PROJECT_ROOT / "cactus" + lib_path = cactus_dir / "build" / "libcactus.a" + + print_color(YELLOW, "Building Cactus library...") + build_script = cactus_dir / "build.sh" if not build_script.exists(): print_color(RED, f"Error: build.sh not found at {build_script}") return 1 - - result = run_command(str(build_script), cwd=PROJECT_ROOT / "apple", check=False) + result = run_command(str(build_script), cwd=cactus_dir, check=False) if result.returncode != 0: - print_color(RED, "Apple build failed") + print_color(RED, "Failed to build cactus library") return 1 - print_color(GREEN, "Apple build complete!") - return 0 - + sdl2 = _detect_sdl2() + if sdl2[0]: + print_color(GREEN, "SDL2 found - building with live audio support") + else: + print_color(YELLOW, "SDL2 not found - live mic recording will be disabled") + print_color(YELLOW, "Install SDL2 for live mic support: brew install sdl2 (macOS)") -def cmd_build_android(args): - """Build Cactus for Android.""" - print_color(BLUE, "Building Cactus for Android...") - print("=" * 32) + rc = build_binary("chat", lib_path, sdl2=sdl2) + if rc != 0: + return rc - build_script = PROJECT_ROOT / "android" / "build.sh" - if not build_script.exists(): - print_color(RED, f"Error: build.sh not found at {build_script}") - return 1 + asr_cpp = PROJECT_ROOT / "cactus-engine" / "tests" / "asr.cpp" + if asr_cpp.exists(): + rc = build_binary("asr", lib_path, sdl2=sdl2) + if rc != 0: + return rc - result = run_command(str(build_script), cwd=PROJECT_ROOT / "android", check=False) - if result.returncode != 0: - print_color(RED, "Android build failed") - return 1 + print_color(GREEN, "Cactus library built successfully!") + print(f"Library location: {lib_path}") - print_color(GREEN, "Android build complete!") return 0 -def cmd_build_flutter(args): - """Build Cactus for Flutter (iOS, macOS, Android).""" - print_color(BLUE, "Building Cactus for Flutter...") - print("=" * 32) +def _build_with_script(subdir, title): + """Run a platform build.sh script from the given subdirectory.""" + print_color(BLUE, f"{title}...") + + if subdir == "apple" and platform.system() != "Darwin": + print_color(RED, "Error: Apple builds require macOS") + return 1 - build_script = PROJECT_ROOT / "flutter" / "build.sh" + build_script = PROJECT_ROOT / subdir / "build.sh" if not build_script.exists(): print_color(RED, f"Error: build.sh not found at {build_script}") return 1 - result = run_command(str(build_script), cwd=PROJECT_ROOT / "flutter", check=False) + result = run_command(str(build_script), cwd=PROJECT_ROOT / subdir, check=False) if result.returncode != 0: - print_color(RED, "Flutter build failed") + print_color(RED, f"{title} failed") return 1 - print_color(GREEN, "Flutter build complete!") - print() - print("Output:") - print(f" flutter/libcactus.so") - print(f" flutter/cactus-ios.xcframework") - print(f" flutter/cactus-macos.xcframework") + print_color(GREEN, f"{title} complete!") return 0 -def cmd_build_python(args): +def cmd_build_python(): """Build Cactus shared library for Python FFI.""" print_color(BLUE, "Building Cactus for Python...") - print("=" * 30) if not check_command('cmake'): print_color(RED, "Error: CMake is not installed") diff --git a/python/cactus/cli/convert.py b/python/cactus/cli/convert.py index cb1d20c76..c6a3fa78a 100644 --- a/python/cactus/cli/convert.py +++ b/python/cactus/cli/convert.py @@ -1,264 +1,38 @@ -from __future__ import annotations - -import argparse -import shutil -from dataclasses import dataclass -from pathlib import Path - -from .common import ( - GREEN, - PROJECT_ROOT, - RED, - YELLOW, - get_weights_dir, - print_color, -) -from .transpile import cmd_transpile, resolve_model_id_alias -from cactus.transpile.component_plan import infer_component_plan_from_output - - -_DEFAULT_MULTIMODAL_PROMPT = ( - "Respond with 2 lines. The first should be a description of the image, " - "and the second should be a transcription of the audio" -) -_DEFAULT_TEXT_PROMPT = "Hello" - - -@dataclass(frozen=True) -class _ConvertTranspileSpec: - task: str - components: tuple[str, ...] = () - needs_image: bool = False - needs_audio: bool = False - force_component_pipeline: bool = False - - -def _default_multimodal_asset_args() -> tuple[list[str], str | None]: - assets_dir = PROJECT_ROOT / "cactus-engine" / "tests" / "assets" - image_file = assets_dir / "test_monkey.png" - audio_file = assets_dir / "test.wav" - image_args = [str(image_file)] if image_file.exists() else [] - audio_arg = str(audio_file) if audio_file.exists() else None - return image_args, audio_arg - - -def _default_audio_asset_arg() -> str | None: - _, audio_file = _default_multimodal_asset_args() - return audio_file - - -def _transpile_spec_for_convert(*, task: str, plan) -> _ConvertTranspileSpec: - if task != "auto": - if plan is not None and task == plan.task: - return _ConvertTranspileSpec( - task=task, - components=tuple(plan.components or ()), - needs_image=bool(plan.needs_image), - needs_audio=bool(plan.needs_audio), - force_component_pipeline=bool(plan.force_component_pipeline), - ) - return _ConvertTranspileSpec( - task=task, - needs_image=task == "multimodal_causal_lm_logits", - needs_audio=task - in {"tdt_transcription", "seq2seq_transcription", "ctc_logits", "encoder_hidden_states", "multimodal_causal_lm_logits"}, - force_component_pipeline=task in {"tdt_transcription", "seq2seq_transcription", "multimodal_causal_lm_logits"}, - ) - - if plan is None: - return _ConvertTranspileSpec(task="causal_lm_logits") - - return _ConvertTranspileSpec( - task=plan.task, - components=tuple(plan.components or ()), - needs_image=bool(plan.needs_image), - needs_audio=bool(plan.needs_audio), - force_component_pipeline=bool(plan.force_component_pipeline), - ) - - -def _default_max_new_tokens_for_task(task: str) -> int: - if task == "seq2seq_transcription": - return 128 - if task == "multimodal_causal_lm_logits": - return 512 - if task == "causal_lm_logits": - return 128 - return 32 - - -def _remove_stale_transpile_artifacts(output_dir: str | Path) -> None: - root = Path(output_dir) - for relative in ( - "components", - "transpile_entrypoints.json", - "raw_ir.json", - "optimized_ir.json", - "graph.cactus", - "graph_bindings.json", - "result.json", - ): - path = root / relative - if path.is_dir(): - shutil.rmtree(path) - elif path.exists(): - path.unlink() - for pattern in ("raw_ir_*.json", "optimized_ir_*.json"): - for path in root.glob(pattern): - if path.is_file(): - path.unlink() +from .common import RED, print_color +from .download import get_weights_dir def cmd_convert(args): """Convert a HuggingFace model to CQ format and transpile it in place.""" - model_id = resolve_model_id_alias(args.model_name) - output_dir = args.output_dir - if output_dir is None: - output_dir = str(get_weights_dir(model_id)) + from .model import resolve_model_id, ensure_bundle, TranspileOptions - bits = getattr(args, "bits", 4) or 4 - token = getattr(args, "token", None) - cache_dir = getattr(args, "cache_dir", None) + model_id = resolve_model_id(args.model_name) + output_dir = args.output_dir or str(get_weights_dir(model_id)) try: - from ..convert.cli import main as cq_main - - cq_args = [ - "convert", - "--model", + ensure_bundle( model_id, - "--out", - str(output_dir), - "--bits", - str(bits), - ] - if token: - cq_args.extend(["--token", token]) - if cache_dir: - cq_args.extend(["--cache-dir", cache_dir]) - cq_args.append("--force") - - cq_main(cq_args) - - task = getattr(args, "task", "auto") or "auto" - prompt = getattr(args, "prompt", None) - image_files = [str(path) for path in (getattr(args, "image_file", None) or []) if str(path).strip()] - audio_file = getattr(args, "audio_file", None) - - plan = infer_component_plan_from_output(str(output_dir), model_id=model_id) - spec = _transpile_spec_for_convert(task=task, plan=plan) - - _remove_stale_transpile_artifacts(output_dir) - - spec_prompt = prompt - spec_image_files = list(image_files) - spec_audio_file = audio_file - component_pipeline = getattr(args, "component_pipeline", "auto") or "auto" - components = getattr(args, "components", None) - - if spec_prompt is None and spec.task == "multimodal_causal_lm_logits": - spec_prompt = _DEFAULT_MULTIMODAL_PROMPT - elif spec_prompt is None and spec.task == "causal_lm_logits": - spec_prompt = _DEFAULT_TEXT_PROMPT - - needs_image = False - needs_audio = False - if spec.task == "multimodal_causal_lm_logits": - needs_image = bool(spec.needs_image) - needs_audio = bool(spec.needs_audio) - if not needs_image and not needs_audio: - needs_image = bool(spec_image_files) - needs_audio = bool(spec_audio_file) - if (needs_image and not spec_image_files) or (needs_audio and not spec_audio_file): - default_images, default_audio = _default_multimodal_asset_args() - if needs_image and not spec_image_files: - spec_image_files = default_images - if needs_audio and not spec_audio_file: - spec_audio_file = default_audio - print_color( - YELLOW, - "Multimodal transpile needs representative media shapes; " - "using bundled tiny test assets.", - ) - if needs_image and not spec_image_files: - print_color( - RED, - "Multimodal transpile requires --image-file for this model.", - ) - return 1 - if needs_audio and not spec_audio_file: - print_color( - RED, - "Multimodal transpile requires --audio-file for this model.", - ) - return 1 - if component_pipeline == "auto" and spec.force_component_pipeline: - component_pipeline = "on" - if components is None and spec.components: - components = ",".join(spec.components) - - used_default_audio = False - if spec.task in {"tdt_transcription", "seq2seq_transcription", "ctc_logits", "encoder_hidden_states"} and not spec_audio_file: - spec_audio_file = _default_audio_asset_arg() - used_default_audio = spec_audio_file is not None - if spec.task in {"tdt_transcription", "seq2seq_transcription", "ctc_logits", "encoder_hidden_states"} and used_default_audio: - print_color( - YELLOW, - f"{spec.task} transpile needs a representative audio shape; " - "using bundled tiny test audio asset.", - ) - elif spec.task in {"tdt_transcription", "seq2seq_transcription", "ctc_logits", "encoder_hidden_states"} and not spec_audio_file: - print_color(RED, f"{spec.task} transpile requires --audio-file.") - return 1 - - artifact_dir = Path(output_dir) - max_new_tokens = getattr(args, "max_new_tokens", None) - if max_new_tokens is None: - max_new_tokens = _default_max_new_tokens_for_task(spec.task) - - extra_args = [ - "--weights-dir", - str(output_dir), - "--artifact-dir", - str(artifact_dir), - "--task", - spec.task, - "--max-new-tokens", - str(max_new_tokens or _default_max_new_tokens_for_task(spec.task)), - "--component-pipeline", - component_pipeline, - ] - if spec_prompt is not None: - extra_args.extend(["--prompt", spec_prompt]) - if components: - extra_args.extend(["--components", str(components)]) - for image_file in spec_image_files: - extra_args.extend(["--image-file", image_file]) - if spec_audio_file: - extra_args.extend(["--audio-file", str(spec_audio_file)]) - if getattr(args, "system_prompt", None): - extra_args.extend(["--system-prompt", str(args.system_prompt)]) - if token: - extra_args.extend(["--token", token]) - if getattr(args, "trust_remote_code", False) or spec.task == "multimodal_causal_lm_logits": - extra_args.append("--trust-remote-code") - if getattr(args, "local_files_only", False): - extra_args.append("--local-files-only") - - transpile_args = argparse.Namespace( - model_id=model_id, - execute_after_transpile=False, - allow_unconverted_weights=False, - extra_args=extra_args, + bits=args.bits or 4, + token=args.token, + cache_dir=args.cache_dir, + reconvert=args.reconvert, + output_dir=output_dir, + transpile=TranspileOptions( + task=args.task or "auto", + prompt=args.prompt, + image_files=[p for p in map(str, args.image_file or []) if p.strip()], + audio_file=args.audio_file, + max_new_tokens=args.max_new_tokens, + component_pipeline=args.component_pipeline or "auto", + components=args.components, + system_prompt=args.system_prompt, + trust_remote_code=args.trust_remote_code, + local_files_only=args.local_files_only, + ), ) - rc = cmd_transpile(transpile_args) - if rc != 0: - return rc - - print_color(GREEN, f"Model converted and transpiled to {output_dir}") return 0 except SystemExit as e: return e.code if e.code else 0 - except Exception as e: + except RuntimeError as e: print_color(RED, f"Conversion error: {e}") return 1 diff --git a/python/cactus/cli/download.py b/python/cactus/cli/download.py index 0e4f230ee..42e2f3ddf 100644 --- a/python/cactus/cli/download.py +++ b/python/cactus/cli/download.py @@ -1,564 +1,38 @@ -"""CQ archive discovery and download helpers. - -The resolver in this module is deliberately independent of argparse and the -rest of the CLI. Keep this logic small and data-shaped so other bindings can port -the same model/repo/combo behavior without inheriting Python CLI details. -""" -from __future__ import annotations - -import hashlib -import json -import os -import re -import shutil -import tarfile -import tempfile -import urllib.parse -import urllib.request -import zipfile -from dataclasses import dataclass +"""Download command and path helpers.""" from pathlib import Path -from typing import Iterable - - -MODALITIES = ("L", "V", "A") -COMBO_RE = re.compile(r"(?:[LVA][1-4])+", re.IGNORECASE) -COMBO_PART_RE = re.compile(r"([LVA])([1-4])", re.IGNORECASE) -ARCHIVE_SUFFIXES = (".zip", ".tar", ".tar.gz", ".tgz") - - -@dataclass(frozen=True) -class CqArchive: - filename: str - combo: dict[str, int] - size: int | None = None - sha256: str | None = None - - -@dataclass(frozen=True) -class CqResolution: - repo_id: str - local_name: str - archive: CqArchive - requested: dict[str, int] - warnings: tuple[str, ...] - available: tuple[CqArchive, ...] - - -def normalize_model_key(model_id: str) -> str: - return str(model_id).strip().lower().replace("_", "-") - - -def default_local_name(model_id: str) -> str: - name = str(model_id).strip().split("/")[-1].lower().replace("_", "-") - return name.removesuffix("-cq") - - -def suggested_cq_repo(model_id: str) -> str: - raw = str(model_id).strip().replace("_", "-") - name = raw.split("/")[-1] - if not name.lower().endswith("-cq"): - name = f"{name}-cq" - return f"Cactus-Compute/{name}" - - -def resolve_cq_repo(model_id: str) -> tuple[str, str]: - raw = str(model_id).strip() - key = normalize_model_key(raw) - - if "/" in raw and key.startswith("cactus-compute/") and key.endswith("-cq"): - return raw, default_local_name(raw) - - raise RuntimeError( - "download-cq expects a Cactus-Compute CQ repo ending in -cq. " - f"If you meant to download CQ weights for {raw}, try {suggested_cq_repo(raw)}." - ) - - -def archive_stem(filename: str) -> str: - for suffix in ARCHIVE_SUFFIXES: - if filename.lower().endswith(suffix): - return filename[: -len(suffix)] - return filename - - -def parse_combo(filename: str) -> dict[str, int]: - stem = archive_stem(Path(filename).name) - if not COMBO_RE.fullmatch(stem): - return {} - combo: dict[str, int] = {} - seen = set() - for modality, bits in COMBO_PART_RE.findall(stem): - modality = modality.upper() - if modality in seen: - return {} - seen.add(modality) - combo[modality] = int(bits) - return combo - - -def is_supported_archive(filename: str) -> bool: - lower = filename.lower() - return any(lower.endswith(suffix) for suffix in ARCHIVE_SUFFIXES) - - -def archives_from_repo_files(repo_files: Iterable[str], sizes: dict[str, int] | None = None, - sha256s: dict[str, str] | None = None) -> tuple[CqArchive, ...]: - sizes = sizes or {} - sha256s = sha256s or {} - archives = [] - for filename in repo_files: - if not is_supported_archive(filename): - continue - combo = parse_combo(filename) - if "L" not in combo: - continue - archives.append(CqArchive(filename=filename, combo=combo, - size=sizes.get(filename), sha256=sha256s.get(filename))) - return tuple(sorted(archives, key=lambda a: a.filename)) - - -def combo_label(combo: dict[str, int]) -> str: - return "".join(f"{m}{combo[m]}" for m in MODALITIES if m in combo) - - -def resolve_archive(repo_id: str, local_name: str, archives: Iterable[CqArchive], - requested: dict[str, int]) -> CqResolution: - available = tuple(archives) - if not available: - raise RuntimeError(f"No CQ archives found in {repo_id}") - - requested = {k.upper(): int(v) for k, v in requested.items() if v is not None} - repo_modalities = {m for archive in available for m in archive.combo} - effective = {} - warnings = [] - for modality in MODALITIES: - if modality not in requested: - continue - if modality not in repo_modalities: - warnings.append( - f"Requested {modality}{requested[modality]}, but {repo_id} has no {modality} modality; ignoring it." - ) - continue - effective[modality] = requested[modality] - - matches = [ - archive for archive in available - if all(archive.combo.get(modality) == bits for modality, bits in effective.items()) - ] - if not matches: - wanted = combo_label(effective) or combo_label(requested) - choices = ", ".join(combo_label(a.combo) for a in available) - raise RuntimeError(f"CQ combo {wanted} not found in {repo_id}. Available combos: {choices}") - - def score(archive: CqArchive) -> tuple[int, int, str]: - # Prefer archives that do not add unrequested modalities when possible, - # then prefer higher precision for unspecified modalities. - extra_modalities = len(set(archive.combo) - set(effective)) - precision_sum = sum(archive.combo.values()) - return (extra_modalities, -precision_sum, archive.filename) - - return CqResolution( - repo_id=repo_id, - local_name=local_name, - archive=sorted(matches, key=score)[0], - requested=requested, - warnings=tuple(warnings), - available=available, - ) - - -def _safe_zip_extract(zip_path: Path, out_dir: Path) -> None: - out_dir = out_dir.resolve() - with zipfile.ZipFile(zip_path, "r") as zf: - for info in zf.infolist(): - target = (out_dir / info.filename).resolve() - if target != out_dir and out_dir not in target.parents: - raise RuntimeError(f"Unsafe path in zip archive: {info.filename}") - mode = (info.external_attr >> 16) & 0o170000 - if mode in (0o120000, 0o10000): - raise RuntimeError(f"Refusing unsafe archive member: {info.filename}") - zf.extractall(out_dir) - - -def _safe_tar_extract(tar_path: Path, out_dir: Path) -> None: - out_dir = out_dir.resolve() - with tarfile.open(tar_path, "r:*") as tf: - for member in tf.getmembers(): - target = (out_dir / member.name).resolve() - if target != out_dir and out_dir not in target.parents: - raise RuntimeError(f"Unsafe path in tar archive: {member.name}") - if member.issym() or member.islnk(): - raise RuntimeError(f"Refusing link in tar archive: {member.name}") - tf.extractall(out_dir) - - -def safe_extract_archive(archive_path: Path, out_dir: Path) -> None: - lower = archive_path.name.lower() - if lower.endswith(".zip"): - _safe_zip_extract(archive_path, out_dir) - elif lower.endswith((".tar", ".tar.gz", ".tgz")): - _safe_tar_extract(archive_path, out_dir) - else: - raise RuntimeError(f"Unsupported CQ archive format: {archive_path.name}") - - -def promote_single_root(output_dir: Path) -> None: - if (output_dir / "config.txt").exists(): - return - - children = [path for path in output_dir.iterdir() if path.name != "__MACOSX"] - if len(children) != 1 or not children[0].is_dir(): - raise RuntimeError("CQ archive must contain config.txt at the archive root or under one top-level directory") - - nested = children[0] - if not (nested / "config.txt").exists(): - raise RuntimeError("CQ archive has one top-level directory, but it does not contain config.txt") - - for child in nested.iterdir(): - target = output_dir / child.name - if target.exists(): - raise RuntimeError(f"Cannot promote archive member over existing path: {target}") - child.rename(target) - nested.rmdir() - - -def validate_extracted_cq(output_dir: Path) -> None: - required = ("config.txt", "token_embeddings.weights", "vocab.txt", "tokenizer_config.txt") - missing = [name for name in required if not (output_dir / name).exists()] - if missing: - raise RuntimeError(f"Downloaded CQ package is missing required file(s): {', '.join(missing)}") - - tokenizer_config = {} - for line in (output_dir / "tokenizer_config.txt").read_text(encoding="utf-8").splitlines(): - line = line.strip() - if not line or line.startswith("#") or "=" not in line: - continue - key, value = line.split("=", 1) - tokenizer_config[key.strip()] = value.strip() - - tokenizer_type = tokenizer_config.get("tokenizer_type", "").lower() - vocab_format = tokenizer_config.get("vocab_format", "").lower() - optional_required = ["special_tokens.json", "tokenizer.json"] - if tokenizer_type == "bpe": - optional_required.append("merges.txt") - missing_sidecars = [name for name in optional_required if not (output_dir / name).exists()] - if missing_sidecars: - raise RuntimeError(f"Downloaded CQ package is missing tokenizer sidecar file(s): {', '.join(missing_sidecars)}") - - -def sha256_file(path: Path) -> str: - h = hashlib.sha256() - with path.open("rb") as f: - for chunk in iter(lambda: f.read(1024 * 1024), b""): - h.update(chunk) - return h.hexdigest() - - -def verify_archive_sha256(archive_path: Path, expected_sha256: str | None) -> None: - if not expected_sha256: - return - actual = sha256_file(archive_path) - if actual.lower() != expected_sha256.lower(): - raise RuntimeError( - f"Downloaded archive checksum mismatch for {archive_path.name}: " - f"expected {expected_sha256}, got {actual}" - ) - - -def _download_with_urllib(resolution: CqResolution, *, token=None, cache_dir=None, revision=None) -> Path: - quoted_repo = urllib.parse.quote(resolution.repo_id, safe="") - quoted_file = urllib.parse.quote(resolution.archive.filename) - revision = revision or "main" - url = f"https://huggingface.co/{resolution.repo_id}/resolve/{urllib.parse.quote(str(revision), safe='')}/{quoted_file}" - - cache_root = Path(cache_dir).expanduser() if cache_dir else Path(tempfile.gettempdir()) / "cactus-cq-cache" - archive_dir = cache_root / quoted_repo / str(revision) - archive_dir.mkdir(parents=True, exist_ok=True) - archive_path = archive_dir / Path(resolution.archive.filename).name - if archive_path.exists() and ( - resolution.archive.size is None or archive_path.stat().st_size == resolution.archive.size - ): - return archive_path - - headers = {"User-Agent": "cactus-cq-downloader"} - if token: - headers["Authorization"] = f"Bearer {token}" - request = urllib.request.Request(url, headers=headers) - tmp_path = archive_path.with_suffix(archive_path.suffix + ".part") - with urllib.request.urlopen(request, timeout=60) as response, tmp_path.open("wb") as out: - shutil.copyfileobj(response, out, length=1024 * 1024) - tmp_path.rename(archive_path) - return archive_path - - -def write_download_metadata(output_dir: Path, resolution: CqResolution, archive_path: Path) -> None: - metadata = { - "repo_id": resolution.repo_id, - "local_name": resolution.local_name, - "archive": resolution.archive.filename, - "combo": resolution.archive.combo, - "requested": resolution.requested, - "warnings": list(resolution.warnings), - "archive_size": resolution.archive.size if resolution.archive.size is not None else archive_path.stat().st_size, - "archive_sha256": resolution.archive.sha256, - } - if metadata["archive_sha256"] is None and os.getenv("CACTUS_CQ_HASH_DOWNLOAD", "") == "1": - metadata["archive_sha256"] = sha256_file(archive_path) - (output_dir / ".cactus_cq_download.json").write_text(json.dumps(metadata, indent=2) + "\n", encoding="utf-8") - - -def download_cq_archive(resolution: CqResolution, output_dir: Path, *, token=None, - cache_dir=None, revision=None, force=False, dry_run=False) -> Path: - if dry_run: - return output_dir - - if output_dir.exists() and force: - shutil.rmtree(output_dir) - if output_dir.exists() and (output_dir / "config.txt").exists(): - return output_dir - if output_dir.exists() and any(output_dir.iterdir()): - raise RuntimeError(f"Output directory already exists and is not a complete CQ package: {output_dir}") - - try: - from huggingface_hub import hf_hub_download - archive_path = Path(hf_hub_download( - repo_id=resolution.repo_id, - filename=resolution.archive.filename, - repo_type="model", - token=token, - cache_dir=cache_dir, - revision=revision, - )) - except ImportError: - archive_path = _download_with_urllib( - resolution, - token=token, - cache_dir=cache_dir, - revision=revision, - ) - verify_archive_sha256(archive_path, resolution.archive.sha256) - - output_dir.parent.mkdir(parents=True, exist_ok=True) - with tempfile.TemporaryDirectory(prefix=f".{output_dir.name}.", dir=str(output_dir.parent)) as tmp: - tmp_dir = Path(tmp) - safe_extract_archive(archive_path, tmp_dir) - promote_single_root(tmp_dir) - validate_extracted_cq(tmp_dir) - write_download_metadata(tmp_dir, resolution, archive_path) - if output_dir.exists(): - shutil.rmtree(output_dir) - tmp_dir.rename(output_dir) - - return output_dir - - -def list_hf_cq_archives(repo_id: str, *, token=None, revision=None) -> tuple[CqArchive, ...]: - try: - from huggingface_hub import HfApi - except ImportError: - quoted = urllib.parse.quote(repo_id, safe="/") - url = f"https://huggingface.co/api/models/{quoted}?blobs=true" - if revision: - url += f"&revision={urllib.parse.quote(str(revision), safe='')}" - headers = {"User-Agent": "cactus-cq-downloader"} - if token: - headers["Authorization"] = f"Bearer {token}" - req = urllib.request.Request(url, headers=headers) - with urllib.request.urlopen(req, timeout=30) as response: - data = json.load(response) - files = [] - sizes = {} - sha256s = {} - for sibling in data.get("siblings", []): - filename = sibling.get("rfilename") - if not filename: - continue - files.append(filename) - lfs = sibling.get("lfs") or {} - if "size" in lfs: - sizes[filename] = int(lfs["size"]) - elif "size" in sibling: - sizes[filename] = int(sibling["size"]) - if "sha256" in lfs: - sha256s[filename] = lfs["sha256"] - return archives_from_repo_files(files, sizes=sizes, sha256s=sha256s) - - info = HfApi().model_info(repo_id, revision=revision, token=token, files_metadata=True) - files = [] - sizes = {} - sha256s = {} - for sibling in info.siblings: - filename = sibling.rfilename - files.append(filename) - size = getattr(sibling, "size", None) - if size is not None: - sizes[filename] = int(size) - lfs = getattr(sibling, "lfs", None) - if isinstance(lfs, dict): - if "sha256" in lfs: - sha256s[filename] = lfs["sha256"] - if "size" in lfs: - sizes[filename] = int(lfs["size"]) - return archives_from_repo_files(files, sizes=sizes, sha256s=sha256s) - - -# --------------------------------------------------------------------------- -# Path helpers and convenience entry points -# --------------------------------------------------------------------------- _PROJECT_ROOT = Path(__file__).resolve().parent.parent.parent.parent -def get_model_dir_name(model_id: str) -> str: +def get_model_dir_name(model_id): """Convert HuggingFace model ID to local directory name.""" return model_id.split("/")[-1].lower() -def get_weights_dir(model_id: str) -> Path: +def get_weights_dir(model_id): """Return ``/weights/``.""" - if "silero-vad" in model_id.lower(): - return _PROJECT_ROOT / "weights" / "silero-vad" return _PROJECT_ROOT / "weights" / get_model_dir_name(model_id) -def download_legacy_fp16_zip(model_id: str, weights_dir: Path) -> bool: - """Download pre-converted FP16 weights zip from Cactus-Compute (legacy fallback). - - Returns True on success, False if unavailable. - """ - try: - from huggingface_hub import hf_hub_download, list_repo_files - except ImportError: - return False - - model_name = get_model_dir_name(model_id) - repo_id = f"Cactus-Compute/{model_id.split('/')[-1]}" - - try: - apple_zip = f"{model_name}-fp16-apple.zip" - standard_zip = f"{model_name}-fp16.zip" - repo_files = list_repo_files(repo_id, repo_type="model") - - zip_file = None - if f"weights/{apple_zip}" in repo_files: - zip_file = apple_zip - elif f"weights/{standard_zip}" in repo_files: - zip_file = standard_zip - else: - return False - - print(f"Downloading {repo_id}/{zip_file} ...") - zip_path = hf_hub_download(repo_id=repo_id, filename=f"weights/{zip_file}", repo_type="model") - - weights_dir.mkdir(parents=True, exist_ok=True) - with zipfile.ZipFile(zip_path, "r") as zf: - zf.extractall(weights_dir) - - if not (weights_dir / "config.txt").exists(): - if weights_dir.exists(): - shutil.rmtree(weights_dir) - return False - - print(f"Model ready at {weights_dir}") - return True - except Exception: - if weights_dir.exists(): - shutil.rmtree(weights_dir) - return False - - -def ensure_model(model_id: str) -> Path: +def ensure_model(model_id): """Return the weights directory, downloading CQ weights if necessary. - Tries CQ first, falls back to legacy FP16 zip. - Raises ``RuntimeError`` if the model cannot be obtained. + Public API — re-exported by ``cactus/__init__.py``. """ - weights_dir = get_weights_dir(model_id) - if (weights_dir / "config.txt").exists(): - return weights_dir - - try: - cq_repo = suggested_cq_repo(model_id) - archives = list_hf_cq_archives(cq_repo) - if archives: - resolution = resolve_archive(cq_repo, default_local_name(model_id), archives, {"L": 4}) - download_cq_archive(resolution, weights_dir) - return weights_dir - except Exception: - pass - - if not download_legacy_fp16_zip(model_id, weights_dir): - raise RuntimeError(f"Could not download model {model_id}") - return weights_dir + from .model import ensure_weights + return ensure_weights(model_id) def cmd_download(args): - """Download CQ model weights from Cactus-Compute.""" - from .common import print_color, RED, GREEN, YELLOW, get_effective_weights_dir - - model_id = args.model_id - weights_dir = get_effective_weights_dir(model_id, args) - reconvert = getattr(args, 'reconvert', False) - token = getattr(args, 'token', None) - cache_dir = getattr(args, 'cache_dir', None) - - if reconvert and weights_dir.exists(): - print_color(YELLOW, f"Removing cached weights for reconversion...") - shutil.rmtree(weights_dir) - - if weights_dir.exists() and (weights_dir / "config.txt").exists(): - print_color(GREEN, f"Model weights found at {weights_dir}") - return 0 - - print() - print_color(YELLOW, f"Model weights not found. Downloading {model_id}...") - print("=" * 45) - - if not reconvert: - cq_repo_id = model_id if (model_id.lower().endswith('-cq') and '/' in model_id) else suggested_cq_repo(model_id) - bits = getattr(args, 'bits', 4) or 4 - requested = {"L": bits} - - try: - archives = list_hf_cq_archives(cq_repo_id, token=token) - if archives: - local_name = get_model_dir_name(model_id) - resolution = resolve_archive(cq_repo_id, local_name, archives, requested) - for warning in resolution.warnings: - print_color(YELLOW, f" {warning}") - size_text = f" ({resolution.archive.size / (1024 * 1024):.1f} MiB)" if resolution.archive.size else "" - print(f" Downloading {resolution.archive.filename} [{combo_label(resolution.archive.combo)}]{size_text}") - download_cq_archive(resolution, weights_dir, token=token, cache_dir=cache_dir) - print_color(GREEN, f"CQ model ready at {weights_dir}") - return 0 - except Exception as cq_exc: - print(f" CQ weights not available ({cq_exc})") - - print_color(RED, f"No CQ weights found for {model_id}") - print() - print("To convert from HuggingFace, re-run with --reconvert:") - print(f" cactus download {model_id} --reconvert") - print() - print("Or convert directly:") - print(f" cactus convert {model_id}") - return 1 + """Download original model weights from HuggingFace.""" + from .model import resolve_model_id, download_model + from .common import print_color, RED - print_color(YELLOW, f"Converting {model_id} using CQ pipeline...") + model_id = resolve_model_id(args.model_id) try: - from ..convert.cli import main as convert_main - bits = getattr(args, 'bits', 4) or 4 - convert_args = ['convert', '--model', model_id, '--out', str(weights_dir), '--bits', str(bits), '--force'] - if token: - convert_args.extend(['--token', token]) - if cache_dir: - convert_args.extend(['--cache-dir', cache_dir]) - convert_main(convert_args) - print_color(GREEN, f"Model converted and ready at {weights_dir}") + download_model(model_id, token=args.token, cache_dir=args.cache_dir) return 0 - except SystemExit as e: - return e.code if e.code else 0 except Exception as e: - print_color(RED, f"Conversion failed: {e}") + print_color(RED, f"Download failed: {e}") return 1 diff --git a/python/cactus/cli/eval.py b/python/cactus/cli/eval.py index 213f540e6..7206e5aa2 100644 --- a/python/cactus/cli/eval.py +++ b/python/cactus/cli/eval.py @@ -4,79 +4,55 @@ from .common import ( PROJECT_ROOT, - DEFAULT_MODEL_ID, - get_effective_weights_dir, print_color, RED, BLUE, ) -from .download import cmd_download def cmd_eval(args): - model_id = getattr(args, 'model_id', DEFAULT_MODEL_ID) + """Run evaluation scripts from the companion evals repo.""" + from .model import ensure_weights - if PROJECT_ROOT.parent.name != 'evals': + model_id = args.model_id + + if PROJECT_ROOT.parent.name != "evals": print_color(RED, "Skipping internal eval checks: companion repo not found.") return 1 - # Check if cactus library exists lib_path = PROJECT_ROOT / "cactus" / "build" / "libcactus.a" if not lib_path.exists(): print_color(RED, "Error: Cactus library not built. Run 'cactus build' first.") return 1 - class DownloadArgs: - pass - - dlargs = DownloadArgs() - dlargs.model_id = model_id - dlargs.cache_dir = getattr(args, 'cache_dir', None) - dlargs.token = getattr(args, 'token', None) - dlargs.reconvert = getattr(args, 'reconvert', False) - - download_result = cmd_download(dlargs) - if download_result != 0: - return download_result - - weights_dir = get_effective_weights_dir(model_id, args) - extra = getattr(args, 'extra_args', None) or [] - - def extra_has_flag(flag: str) -> bool: - for a in extra: - if a == flag or a.startswith(flag + "="): - return True - return False - - mode_flags = [] - if getattr(args, 'tools', False): mode_flags.append('tools') - if getattr(args, 'llm', False): mode_flags.append('llm') - if getattr(args, 'stt', False): mode_flags.append('stt') - if getattr(args, 'vlm', False): mode_flags.append('vlm') - if getattr(args, 'embed', False): mode_flags.append('embed') - - if len(mode_flags) > 1: - print_color(RED, f"Error: choose only one eval mode flag, got: {' '.join(mode_flags)}") + try: + weights_dir = ensure_weights( + model_id, + token=args.token, + cache_dir=args.cache_dir, + reconvert=args.reconvert, + ) + except RuntimeError as e: + print_color(RED, str(e)) return 1 - mode = mode_flags[0] if mode_flags else "tools" - repo_root = PROJECT_ROOT.parent # evals/ - cwd = repo_root - - if mode == "tools": - eval_runner = repo_root / "tool-evals" / "run_eval_berk.py" - elif mode == "stt": - eval_runner = repo_root / "speech-evals" / "speech_eval.py" - elif mode == "llm": - eval_runner = repo_root / "text-evals" / "perplexity_eval.py" - elif mode == "vlm": - eval_runner = repo_root / "video-evals" / "run_benchmarks.py" - elif mode == "embed": - print_color(RED, f"Error: eval mode '{mode}' is not supported in this repo layout") - return 1 - else: - print_color(RED, f"Error: unknown eval mode '{mode}'") - return 1 + extra = getattr(args, "extra_args", None) or [] + def extra_has_flag(flag): + return any(a == flag or a.startswith(flag + "=") for a in extra) + + mode = args.suite + repo_root = PROJECT_ROOT.parent + + eval_runners = { + "tools": repo_root / "tool-evals" / "run_eval_berk.py", + "stt": repo_root / "speech-evals" / "speech_eval.py", + "llm": repo_root / "text-evals" / "perplexity_eval.py", + "vlm": repo_root / "video-evals" / "run_benchmarks.py", + } + eval_runner = eval_runners.get(mode) + if eval_runner is None: + print_color(RED, f"Error: eval mode '{mode}' is not supported") + return 1 if not eval_runner.exists(): print_color(RED, f"Eval runner not found at {eval_runner}") return 1 @@ -96,20 +72,15 @@ def extra_has_flag(flag: str) -> bool: cmd += ["--model-id", str(model_id)] if mode == "stt" and not extra_has_flag("--dataset-path"): - default_dataset_path = repo_root / "speech-evals" / "dataset-retrieval" - cmd += ["--dataset-path", str(default_dataset_path)] - - if not extra_has_flag("--output-dir"): - if mode == "tools": - default_out = repo_root / "tool-evals" / "results" - elif mode == "stt": - default_out = repo_root / "speech-evals" / "results" - elif mode == "llm": - default_out = repo_root / "text-evals" / "results" - else: - default_out = None - if default_out is not None: - cmd += ["--output-dir", str(default_out)] + cmd += ["--dataset-path", str(repo_root / "speech-evals" / "dataset-retrieval")] + + default_output_dirs = { + "tools": repo_root / "tool-evals" / "results", + "stt": repo_root / "speech-evals" / "results", + "llm": repo_root / "text-evals" / "results", + } + if not extra_has_flag("--output-dir") and mode in default_output_dirs: + cmd += ["--output-dir", str(default_output_dirs[mode])] cmd += extra @@ -117,12 +88,11 @@ def extra_has_flag(flag: str) -> bool: print(" ".join(cmd)) env = os.environ.copy() - if getattr(args, 'no_cloud_tele', False): + if args.no_cloud_tele: env["CACTUS_NO_CLOUD_TELE"] = "1" if mode == "vlm": ffi_dir = str(repo_root / "cactus" / "tools" / "src") existing = env.get("PYTHONPATH", "") env["PYTHONPATH"] = ffi_dir if not existing else (ffi_dir + os.pathsep + existing) - r = subprocess.run(cmd, cwd=str(cwd), env=env) - return r.returncode + return subprocess.run(cmd, cwd=str(repo_root), env=env).returncode diff --git a/python/cactus/cli/model.py b/python/cactus/cli/model.py new file mode 100644 index 000000000..cb3a240f8 --- /dev/null +++ b/python/cactus/cli/model.py @@ -0,0 +1,392 @@ +"""Model resolution, weight management, and bundle preparation.""" +from __future__ import annotations + +import shutil +from dataclasses import dataclass +from pathlib import Path + +from .common import GREEN, PROJECT_ROOT, YELLOW, print_color + + +# ── Model ID aliases (single source of truth) ──────────────────────── + +MODEL_ID_ALIASES = { + "gemma4": "google/gemma-4-E2B-it", + "gemma4-e2b": "google/gemma-4-E2B-it", + "parakeet": "nvidia/parakeet-tdt-0.6b-v3", + "parakeet-tdt": "nvidia/parakeet-tdt-0.6b-v3", + "whisper": "openai/whisper-small", + "qwen": "Qwen/Qwen3-1.7B", + "lfm": "LiquidAI/LFM2-VL-450M", +} + + +def resolve_model_id(raw): + """Normalize alias -> canonical HuggingFace model ID.""" + normalized = (raw or "").strip() + return MODEL_ID_ALIASES.get(normalized.lower(), normalized) + + +# ── Weight download / conversion ────────────────────────────────────── + + +def download_model(model_id, *, token=None, cache_dir=None): + """Download raw model weights from HuggingFace.""" + from .download import get_weights_dir + + weights_dir = get_weights_dir(model_id) + + print() + print_color(YELLOW, f"Downloading {model_id} from HuggingFace...") + print("=" * 45) + + from huggingface_hub import snapshot_download + snapshot_download( + repo_id=model_id, + token=token, + cache_dir=cache_dir, + local_dir=str(weights_dir), + ) + print_color(GREEN, f"Model downloaded to {weights_dir}") + return weights_dir + + +def _try_cq_download(model_id, *, bits, token, cache_dir, weights_dir): + """Try pre-converted CQ weights from Cactus-Compute. Returns path or None.""" + from .download import get_model_dir_name + from .utils import ( + combo_label, + download_cq_archive, + list_hf_cq_archives, + resolve_archive, + suggested_cq_repo, + ) + + cq_repo_id = ( + model_id + if model_id.lower().endswith("-cq") and "/" in model_id + else suggested_cq_repo(model_id) + ) + try: + archives = list_hf_cq_archives(cq_repo_id, token=token) + if not archives: + return None + local_name = get_model_dir_name(model_id) + resolution = resolve_archive(cq_repo_id, local_name, archives, {"L": bits}) + for w in resolution.warnings: + print_color(YELLOW, f" {w}") + size_text = ( + f" ({resolution.archive.size / (1024 * 1024):.1f} MiB)" + if resolution.archive.size + else "" + ) + print(f" Downloading pre-converted {resolution.archive.filename}" + f" [{combo_label(resolution.archive.combo)}]{size_text}") + download_cq_archive(resolution, weights_dir, token=token, cache_dir=cache_dir) + print_color(GREEN, f"CQ model ready at {weights_dir}") + return weights_dir + except Exception as exc: + print(f" Pre-converted CQ not available ({exc})") + return None + + +def _convert_from_source(model_id, *, bits, token, cache_dir, weights_dir): + """Download from HuggingFace and run CQ conversion.""" + print_color(YELLOW, f"Converting {model_id} from HuggingFace source...") + from ..convert.cli import main as cq_main + + cq_args = [ + "convert", "--model", model_id, + "--out", str(weights_dir), + "--bits", str(bits), + "--force", + ] + if token: + cq_args.extend(["--token", token]) + if cache_dir: + cq_args.extend(["--cache-dir", cache_dir]) + cq_main(cq_args) + + print_color(GREEN, f"Model converted and ready at {weights_dir}") + return weights_dir + + +def ensure_weights(model_id, *, bits=4, token=None, cache_dir=None, + reconvert=False, output_dir=None): + """Return path to CQ weights dir, converting as needed.""" + from .download import get_weights_dir + + weights_dir = Path(output_dir) if output_dir else get_weights_dir(model_id) + + if reconvert and weights_dir.exists(): + print_color(YELLOW, "Removing cached weights for reconversion...") + shutil.rmtree(weights_dir) + + if weights_dir.exists() and (weights_dir / "config.txt").exists(): + print_color(GREEN, f"Model weights found at {weights_dir}") + return weights_dir + + if not reconvert: + result = _try_cq_download(model_id, bits=bits, token=token, + cache_dir=cache_dir, weights_dir=weights_dir) + if result is not None: + return result + + return _convert_from_source(model_id, bits=bits, token=token, + cache_dir=cache_dir, weights_dir=weights_dir) + + +# ── Transpile spec helpers ──────────────────────────────────────────── + +_DEFAULT_MULTIMODAL_PROMPT = ( + "Respond with 2 lines. The first should be a description of the image, " + "and the second should be a transcription of the audio" +) +_DEFAULT_TEXT_PROMPT = "Hello" + + +@dataclass(frozen=True) +class _TranspileSpec: + task: str + components: tuple[str, ...] = () + needs_image: bool = False + needs_audio: bool = False + force_component_pipeline: bool = False + + +def _spec_from_plan(plan): + """Convert a ComponentPlan into a _TranspileSpec.""" + return _TranspileSpec( + task=plan.task, + components=tuple(plan.components or ()), + needs_image=bool(plan.needs_image), + needs_audio=bool(plan.needs_audio), + force_component_pipeline=bool(plan.force_component_pipeline), + ) + + +def _infer_transpile_spec(*, task, plan): + """Determine transpile parameters from task + component plan.""" + if task != "auto": + if plan is not None and task == plan.task: + return _spec_from_plan(plan) + return _TranspileSpec( + task=task, + needs_image=task == "multimodal_causal_lm_logits", + needs_audio=task in { + "tdt_transcription", "seq2seq_transcription", + "ctc_logits", "encoder_hidden_states", + "multimodal_causal_lm_logits", + }, + force_component_pipeline=task in { + "tdt_transcription", "seq2seq_transcription", + "multimodal_causal_lm_logits", + }, + ) + + if plan is None: + return _TranspileSpec(task="causal_lm_logits") + + return _spec_from_plan(plan) + + +def _default_max_new_tokens(task): + """Sensible token budget per task type.""" + return { + "seq2seq_transcription": 128, + "multimodal_causal_lm_logits": 512, + "causal_lm_logits": 128, + }.get(task, 32) + + +def _default_multimodal_assets(): + """Return bundled test image/audio paths for multimodal shape capture.""" + assets_dir = PROJECT_ROOT / "cactus-engine" / "tests" / "assets" + image_file = assets_dir / "test_monkey.png" + audio_file = assets_dir / "test.wav" + image_args = [str(image_file)] if image_file.exists() else [] + audio_arg = str(audio_file) if audio_file.exists() else None + return image_args, audio_arg + + +def _default_audio_asset(): + _, audio = _default_multimodal_assets() + return audio + + +def _remove_stale_transpile_artifacts(output_dir): + """Clean old transpile outputs before re-transpiling.""" + for relative in ( + "components", + "transpile_entrypoints.json", + "raw_ir.json", + "optimized_ir.json", + "graph.cactus", + "graph_bindings.json", + "result.json", + ): + path = output_dir / relative + if path.is_dir(): + shutil.rmtree(path) + elif path.exists(): + path.unlink() + for pattern in ("raw_ir_*.json", "optimized_ir_*.json"): + for path in output_dir.glob(pattern): + if path.is_file(): + path.unlink() + + +def _has_transpiled_bundle(path): + """Check if path contains a transpiled bundle.""" + return (path / "components" / "manifest.json").exists() + + +_AUDIO_TASKS = frozenset({ + "tdt_transcription", "seq2seq_transcription", + "ctc_logits", "encoder_hidden_states", +}) + + +# ── Bundle preparation (weights + transpile) ────────────────────────── + + +@dataclass(frozen=True) +class TranspileOptions: + """Transpile-phase parameters for ensure_bundle.""" + task: str = "auto" + prompt: str | None = None + image_files: list[str] | None = None + audio_file: str | None = None + max_new_tokens: int | None = None + component_pipeline: str = "auto" + components: str | None = None + system_prompt: str | None = None + trust_remote_code: bool = False + local_files_only: bool = False + + +def ensure_bundle(model_id, *, bits=4, token=None, cache_dir=None, + reconvert=False, output_dir=None, transpile=None): + """Return path to transpiled bundle, creating it if needed. + """ + from .download import get_weights_dir + from .transpile import cmd_transpile + from cactus.transpile.component_plan import infer_component_plan_from_output + + opts = transpile or TranspileOptions() + + if output_dir is not None: + output_dir = Path(output_dir) + else: + output_dir = get_weights_dir(model_id) + + # Step 1: ensure CQ weights exist + ensure_weights( + model_id, bits=bits, token=token, + cache_dir=cache_dir, reconvert=reconvert, + output_dir=output_dir, + ) + + # Step 2: skip if already transpiled + if _has_transpiled_bundle(output_dir): + return output_dir + + # Step 3: infer transpile spec from converted output + plan = infer_component_plan_from_output(str(output_dir), model_id=model_id) + spec = _infer_transpile_spec(task=opts.task, plan=plan) + _remove_stale_transpile_artifacts(output_dir) + + # Step 4: resolve defaults for prompt, images, audio + spec_prompt = opts.prompt + spec_image_files = list(opts.image_files or []) + spec_audio_file = opts.audio_file + + if spec_prompt is None and spec.task == "multimodal_causal_lm_logits": + spec_prompt = _DEFAULT_MULTIMODAL_PROMPT + elif spec_prompt is None and spec.task == "causal_lm_logits": + spec_prompt = _DEFAULT_TEXT_PROMPT + + effective_component_pipeline = opts.component_pipeline + effective_components = opts.components + + if spec.task == "multimodal_causal_lm_logits": + needs_image = spec.needs_image + needs_audio = spec.needs_audio + if not needs_image and not needs_audio: + needs_image = bool(spec_image_files) + needs_audio = bool(spec_audio_file) + if (needs_image and not spec_image_files) or (needs_audio and not spec_audio_file): + default_images, default_audio = _default_multimodal_assets() + if needs_image and not spec_image_files: + spec_image_files = default_images + if needs_audio and not spec_audio_file: + spec_audio_file = default_audio + print_color( + YELLOW, + "Multimodal transpile needs representative media shapes; " + "using bundled tiny test assets.", + ) + if needs_image and not spec_image_files: + raise RuntimeError("Multimodal transpile requires --image-file for this model.") + if needs_audio and not spec_audio_file: + raise RuntimeError("Multimodal transpile requires --audio-file for this model.") + + if effective_component_pipeline == "auto" and spec.force_component_pipeline: + effective_component_pipeline = "on" + if effective_components is None and spec.components: + effective_components = ",".join(spec.components) + + # Handle audio-only tasks + used_default_audio = False + if spec.task in _AUDIO_TASKS and not spec_audio_file: + spec_audio_file = _default_audio_asset() + used_default_audio = spec_audio_file is not None + if spec.task in _AUDIO_TASKS and used_default_audio: + print_color( + YELLOW, + f"{spec.task} transpile needs a representative audio shape; " + "using bundled tiny test audio asset.", + ) + elif spec.task in _AUDIO_TASKS and not spec_audio_file: + raise RuntimeError(f"{spec.task} transpile requires --audio-file.") + + # Step 5: build transpile args and call cmd_transpile + effective_max_new_tokens = opts.max_new_tokens or _default_max_new_tokens(spec.task) + + extra_args = [ + "--weights-dir", str(output_dir), + "--artifact-dir", str(output_dir), + "--task", spec.task, + "--max-new-tokens", str(effective_max_new_tokens), + "--component-pipeline", effective_component_pipeline, + ] + if spec_prompt is not None: + extra_args.extend(["--prompt", spec_prompt]) + if effective_components: + extra_args.extend(["--components", str(effective_components)]) + for img in spec_image_files: + extra_args.extend(["--image-file", img]) + if spec_audio_file: + extra_args.extend(["--audio-file", str(spec_audio_file)]) + if opts.system_prompt: + extra_args.extend(["--system-prompt", str(opts.system_prompt)]) + if token: + extra_args.extend(["--token", token]) + if opts.trust_remote_code or spec.task == "multimodal_causal_lm_logits": + extra_args.append("--trust-remote-code") + if opts.local_files_only: + extra_args.append("--local-files-only") + + import argparse + transpile_ns = argparse.Namespace( + model_id=model_id, + execute_after_transpile=False, + allow_unconverted_weights=False, + extra_args=extra_args, + ) + rc = cmd_transpile(transpile_ns) + if rc != 0: + raise RuntimeError(f"Transpilation failed for {model_id}") + + print_color(GREEN, f"Model converted and transpiled to {output_dir}") + return output_dir diff --git a/python/cactus/cli/run.py b/python/cactus/cli/run.py index 52957ac46..73ce1ec1c 100644 --- a/python/cactus/cli/run.py +++ b/python/cactus/cli/run.py @@ -7,8 +7,8 @@ from .common import PROJECT_ROOT, print_color, RED, GREEN -def _resolve_bundle_dir(model_id: str) -> Path | None: - """Treat model_id as a local bundle dir; return its root if it is a transpiled bundle.""" +def _resolve_bundle_dir(model_id): + """Return bundle root if model_id points to a local transpiled bundle.""" path = Path(model_id).expanduser() if not path.exists() or not path.is_dir(): return None @@ -19,42 +19,53 @@ def _resolve_bundle_dir(model_id: str) -> Path | None: return None -def _ensure_chat_binary() -> Path | None: - chat = PROJECT_ROOT / "cactus-engine" / "tests" / "build" / "chat" - if chat.exists(): - return chat - print_color(RED, "Error: chat binary not found. Run `cactus build` first.") - return None - - def cmd_run(args): - """Run a transpiled Cactus bundle through the libcactus-backed chat binary.""" - if getattr(args, 'no_cloud_tele', False): + """Run a model — auto-converts if needed.""" + from .model import resolve_model_id, ensure_bundle + + if args.no_cloud_tele: os.environ["CACTUS_NO_CLOUD_TELE"] = "1" bundle_dir = _resolve_bundle_dir(args.model_id) if bundle_dir is None: - print_color(RED, - f"Error: {args.model_id} is not a transpiled bundle. " - "Run `cactus convert ` to produce one.") - return 1 + model_id = resolve_model_id(args.model_id) + try: + bundle_dir = ensure_bundle( + model_id, + token=args.token, + cache_dir=args.cache_dir, + reconvert=args.reconvert, + ) + except RuntimeError as e: + print_color(RED, str(e)) + return 1 - chat = _ensure_chat_binary() - if chat is None: + chat = PROJECT_ROOT / "cactus-engine" / "tests" / "build" / "chat" + if not chat.exists(): + print_color(RED, "Chat binary not found. Run `cactus build` first.") return 1 cmd = [str(chat), str(bundle_dir)] - for flag, value in (("--system", getattr(args, 'system', None)), - ("--prompt", getattr(args, 'prompt', None)), - ("--image", getattr(args, 'image', None)), - ("--audio", getattr(args, 'audio', None) or getattr(args, 'audio_file', None))): + for flag, value in ( + ("--system", args.system), + ("--prompt", args.prompt), + ("--image", args.image), + ("--audio", args.audio or args.audio_file), + ): if value: - cmd.extend([flag, str(Path(value).expanduser().resolve()) if flag in ("--image", "--audio") else str(value)]) - if getattr(args, 'thinking', False): + cmd.extend([ + flag, + str(Path(value).expanduser().resolve()) + if flag in ("--image", "--audio") + else str(value), + ]) + if args.thinking: cmd.append("--thinking") if sys.stdout.isatty(): - os.system('clear' if platform.system() != 'Windows' else 'cls') + subprocess.run(["clear" if platform.system() != "Windows" else "cls"], + shell=False, check=False, + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) print_color(GREEN, f"Starting Cactus Chat with model: {bundle_dir}") print() diff --git a/python/cactus/cli/runtime.py b/python/cactus/cli/runtime.py new file mode 100644 index 000000000..834e4b499 --- /dev/null +++ b/python/cactus/cli/runtime.py @@ -0,0 +1,142 @@ +"""Build-system helpers for the C++ runtime.""" +from __future__ import annotations + +import platform +import shutil +import subprocess + +from .common import PROJECT_ROOT, YELLOW, print_color + + +# ── Static library ──────────────────────────────────────────────────── + + +def _static_library_path(): + return PROJECT_ROOT / "cactus" / "build" / "libcactus.a" + + +def ensure_library(): + """Build libcactus.a if it doesn't exist. Return its path.""" + lib = _static_library_path() + if lib.exists(): + return lib + + build_script = PROJECT_ROOT / "cactus" / "build.sh" + if not build_script.exists(): + raise RuntimeError(f"The Cactus build script is missing: {build_script}") + + result = subprocess.run([str(build_script)], cwd=PROJECT_ROOT / "cactus") + if result.returncode != 0: + raise RuntimeError("Failed to build the Cactus static runtime") + + if not lib.exists(): + raise RuntimeError( + "The Cactus build completed, but the static library was not produced.\n" + f"Expected: {lib}" + ) + return lib + + +# ── Shared dynamic library (for transpiler Python FFI) ──────────────── + + +def _python_runtime_library_path(): + suffix = ".dylib" if platform.system() == "Darwin" else ".so" + return PROJECT_ROOT / "cactus" / "build" / f"libcactus{suffix}" + + +def _public_cactus_api_symbols(static_lib): + """Extract cactus_* symbols from the static library.""" + if platform.system() == "Darwin": + command = ["nm", "-gU", str(static_lib)] + else: + command = ["nm", "-g", "--defined-only", str(static_lib)] + + result = subprocess.run(command, capture_output=True, text=True) + if result.returncode != 0: + raise RuntimeError( + "Failed to inspect the Cactus static runtime symbols.\n" + f"Command: {' '.join(command)}\n" + f"{result.stderr.strip()}" + ) + + seen = set() + symbols = [] + for line in result.stdout.splitlines(): + parts = line.split() + if not parts: + continue + symbol = parts[-1].strip() + normalized = symbol[1:] if symbol.startswith("_") else symbol + if normalized.startswith("cactus_") and symbol not in seen: + seen.add(symbol) + symbols.append(symbol) + if not symbols: + raise RuntimeError( + f"Could not find any public cactus_* symbols in {static_lib}" + ) + return symbols + + +def _link_python_runtime_library(*, static_lib, library_path): + """Link static lib into a shared .dylib/.so with exported symbols.""" + build_dir = library_path.parent + build_dir.mkdir(parents=True, exist_ok=True) + if library_path.exists(): + library_path.unlink() + + exported_symbols = _public_cactus_api_symbols(static_lib) + if platform.system() == "Darwin": + compiler = shutil.which("clang++") or shutil.which("c++") + if not compiler: + raise RuntimeError("Failed to find a C++ compiler for linking libcactus.dylib") + command = [ + compiler, + "-dynamiclib", + "-o", str(library_path), + *[f"-Wl,-u,{s}" for s in exported_symbols], + str(static_lib), + "-Wl,-install_name,@rpath/libcactus.dylib", + "-lcurl", + "-framework", "Accelerate", + "-framework", "CoreML", + "-framework", "Foundation", + "-framework", "Security", + "-framework", "SystemConfiguration", + "-framework", "CFNetwork", + ] + else: + compiler = shutil.which("g++") or shutil.which("c++") + if not compiler: + raise RuntimeError("Failed to find a C++ compiler for linking libcactus.so") + command = [ + compiler, + "-shared", + "-o", str(library_path), + *[f"-Wl,--undefined={s}" for s in exported_symbols], + str(static_lib), + "-lcurl", "-pthread", "-ldl", "-lm", + ] + + result = subprocess.run(command, cwd=build_dir) + if result.returncode != 0 or not library_path.exists(): + raise RuntimeError(f"Failed to link the Cactus shared runtime: {library_path}") + + +def ensure_python_runtime_library(): + """Build shared .dylib/.so for the transpiler. Return its path.""" + library_path = _python_runtime_library_path() + static_lib = _static_library_path() + + if ( + library_path.exists() + and static_lib.exists() + and library_path.stat().st_mtime >= static_lib.stat().st_mtime + ): + return library_path + + print_color(YELLOW, "Preparing Cactus shared runtime for transpiler...") + if not static_lib.exists(): + static_lib = ensure_library() + _link_python_runtime_library(static_lib=static_lib, library_path=library_path) + return library_path diff --git a/python/cactus/cli/test.py b/python/cactus/cli/test.py index 711afd17d..1fe8fbea2 100644 --- a/python/cactus/cli/test.py +++ b/python/cactus/cli/test.py @@ -3,75 +3,64 @@ from .common import ( PROJECT_ROOT, - DEFAULT_TEST_MODEL_ID, - get_weights_dir, print_color, RED, YELLOW, BLUE, ) -from .download import cmd_download +LAYERS = { + "kernel": PROJECT_ROOT / "cactus-kernels", + "graph": PROJECT_ROOT / "cactus-graph", +} -def cmd_test(args): - """Run the Cactus test suite.""" - print_color(BLUE, "Running test suite...") - print("=" * 20) +ENGINE_DIR = PROJECT_ROOT / "cactus-engine" - model_id = getattr(args, 'model', DEFAULT_TEST_MODEL_ID) - if getattr(args, 'ios', False) and not getattr(args, 'reconvert', False): - print_color( - YELLOW, - "Warning: iOS tests without --reconvert may use stale or inconsistent local weights. " - "If tests fail unexpectedly, rerun with --reconvert." - ) +def discover_suites(): + suites = [] + for name, path in LAYERS.items(): + if (path / "test.sh").exists(): + suites.append(name) + for f in sorted((ENGINE_DIR / "tests").glob("test_*.cpp")): + name = f.stem.removeprefix("test_") + if name != "utils": + suites.append(name) + return suites - from types import SimpleNamespace - dl_args = SimpleNamespace( - model_id=model_id, - reconvert=getattr(args, 'reconvert', False), - cache_dir=None, - token=getattr(args, 'token', None), - ) - if cmd_download(dl_args) != 0: - print_color(RED, "Failed to download model weights") - return 1 - test_filter = getattr(args, 'only', None) - for _test_name in ['llm', 'vlm', 'stt', 'embed', 'rag', 'graph', 'index', 'kernel', 'kv_cache', 'performance']: - if getattr(args, _test_name, False): - test_filter = _test_name - break +def cmd_test(args): + from .model import ensure_weights - if test_filter == "kernel": - test_script = PROJECT_ROOT / "cactus-kernels" / "test.sh" - test_cwd = PROJECT_ROOT / "cactus-kernels" - elif test_filter in ("graph", "kv_cache"): - test_script = PROJECT_ROOT / "cactus-graph" / "test.sh" - test_cwd = PROJECT_ROOT / "cactus-graph" - else: - test_script = PROJECT_ROOT / "cactus-engine" / "test.sh" - test_cwd = PROJECT_ROOT / "cactus-engine" + print_color(BLUE, "Running test suite...") - if not test_script.exists(): - print_color(RED, f"Error: Test script not found at {test_script}") + if args.ios and not args.reconvert: + print_color(YELLOW, "Warning: iOS tests without --reconvert may use stale weights.") + + try: + weights_dir = ensure_weights(args.model, token=args.token, reconvert=args.reconvert) + except RuntimeError as e: + print_color(RED, f"Failed to download model weights: {e}") return 1 - cmd = [str(test_script)] + suite = args.suite + test_cwd = LAYERS.get(suite, ENGINE_DIR) + test_script = test_cwd / "test.sh" - weights_dir = get_weights_dir(model_id) - cmd.extend(["--model", str(weights_dir)]) + if not test_script.exists(): + print_color(RED, f"Test script not found: {test_script}") + return 1 + cmd = [str(test_script), "--model", str(weights_dir)] if args.android: cmd.append("--android") if args.ios: cmd.append("--ios") - if test_filter: - cmd.extend(["--only", test_filter]) + if suite: + cmd.extend(["--only", suite]) + env = os.environ.copy() - if getattr(args, 'enable_telemetry', False): + if args.enable_telemetry: env.pop("CACTUS_NO_CLOUD_TELE", None) else: env["CACTUS_NO_CLOUD_TELE"] = "1" - result = subprocess.run(cmd, cwd=test_cwd, env=env) - return result.returncode + return subprocess.run(cmd, cwd=test_cwd, env=env).returncode diff --git a/python/cactus/cli/transcribe.py b/python/cactus/cli/transcribe.py index d5f212a86..043cd0fbd 100644 --- a/python/cactus/cli/transcribe.py +++ b/python/cactus/cli/transcribe.py @@ -1,20 +1,17 @@ import os -import subprocess import platform +import subprocess +import sys from pathlib import Path from .common import ( PROJECT_ROOT, - DEFAULT_TEST_MODEL_ID, + DEFAULT_ASR_MODEL_ID, check_command, run_command, - get_weights_dir, print_color, - RED, GREEN, YELLOW, BLUE, + RED, GREEN, BLUE, ) -from .download import cmd_download - -DEFAULT_ASR_MODEL_ID = DEFAULT_TEST_MODEL_ID def _pick_android_device_id(preferred_device=None): @@ -179,13 +176,14 @@ def _cmd_transcribe_ios(weights_dir, audio_file, args): env["CACTUS_ASR_AUDIO_SOURCE"] = str(audio_path) env["CACTUS_ASR_AUDIO_FILE"] = audio_path.name - cmd = [str(ios_script), transcribe_model_id, transcribe_model_id, "snakers4/silero-vad"] + cmd = [str(ios_script), transcribe_model_id, transcribe_model_id] print_color(BLUE, "Running iOS transcription...") return subprocess.run(cmd, cwd=PROJECT_ROOT / "tests" / "ios", env=env).returncode def cmd_transcribe(args): - """Download ASR model if needed and start transcription.""" + """Convert ASR model if needed and start transcription.""" + from .model import resolve_model_id, ensure_weights from .config_utils import CactusConfig from .common import prompt_for_api_key @@ -195,54 +193,65 @@ def cmd_transcribe(args): if api_key: os.environ["CACTUS_CLOUD_KEY"] = api_key - model_id = getattr(args, 'model_id', DEFAULT_ASR_MODEL_ID) - audio_file = getattr(args, 'audio_file', None) + model_id = resolve_model_id(args.model_id) + audio_file = args.audio_file - if getattr(args, 'no_cloud_tele', False): + if args.no_cloud_tele: os.environ["CACTUS_NO_CLOUD_TELE"] = "1" - if getattr(args, 'force_handoff', False): + if args.force_handoff: os.environ["CACTUS_FORCE_HANDOFF"] = "1" else: os.environ.pop("CACTUS_FORCE_HANDOFF", None) - audio_extensions = ('.wav', '.mp3', '.flac', '.ogg', '.m4a', '.aac') + audio_extensions = (".wav", ".mp3", ".flac", ".ogg", ".m4a", ".aac") if model_id and model_id.lower().endswith(audio_extensions): audio_file = model_id model_id = DEFAULT_ASR_MODEL_ID - args.model_id = model_id + # Get converted weights (auto-converts if needed) local_path = Path(model_id) if local_path.exists() and (local_path / "config.txt").exists(): weights_dir = local_path print_color(GREEN, f"Using local model: {weights_dir}") else: - download_result = cmd_download(args) - if download_result != 0: - return download_result - weights_dir = get_weights_dir(model_id) - - if getattr(args, 'android', False) and getattr(args, 'ios', False): + try: + weights_dir = ensure_weights( + model_id, + token=args.token, + cache_dir=args.cache_dir, + reconvert=args.reconvert, + ) + except RuntimeError as e: + print_color(RED, str(e)) + return 1 + + # Platform dispatch + if args.android and args.ios: print_color(RED, "Error: choose only one of --android or --ios") return 1 - if getattr(args, 'android', False): + if args.android: return _cmd_transcribe_android(weights_dir, audio_file, args) - if getattr(args, 'ios', False): + if args.ios: return _cmd_transcribe_ios(weights_dir, audio_file, args) + # Desktop: find asr binary (must be pre-built via `cactus build`) asr_binary = PROJECT_ROOT / "cactus-engine" / "tests" / "build" / "asr" if not asr_binary.exists(): - print_color(RED, "Error: ASR binary not built. Run 'cactus build' first.") + print_color(RED, "ASR binary not found. Run `cactus build` first.") return 1 - os.system('clear' if platform.system() != 'Windows' else 'cls') + if sys.stdout.isatty(): + subprocess.run(["clear" if platform.system() != "Windows" else "cls"], + shell=False, check=False, + stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) print_color(GREEN, f"Starting Cactus ASR with model: {model_id}") print() cmd_args = [str(asr_binary), str(weights_dir)] if audio_file: cmd_args.append(audio_file) - if hasattr(args, 'language') and args.language: - cmd_args.extend(['--language', args.language]) + if args.language: + cmd_args.extend(["--language", args.language]) os.execv(str(asr_binary), cmd_args) diff --git a/python/cactus/cli/transpile.py b/python/cactus/cli/transpile.py index 6b930d95b..2b16cd053 100644 --- a/python/cactus/cli/transpile.py +++ b/python/cactus/cli/transpile.py @@ -1,166 +1,16 @@ from __future__ import annotations import os -import platform -import shutil import subprocess import sys from pathlib import Path -from .common import ( - DEFAULT_MODEL_ID, - PROJECT_ROOT, - RED, - YELLOW, - print_color, -) +from .common import PROJECT_ROOT, RED, YELLOW, print_color from .download import get_weights_dir +from .model import resolve_model_id -MODEL_ID_ALIASES = { - "gemma4": DEFAULT_MODEL_ID, - "gemma4-e2b": DEFAULT_MODEL_ID, - "parakeet": "nvidia/parakeet-tdt-0.6b-v3", - "parakeet-tdt": "nvidia/parakeet-tdt-0.6b-v3", - "whisper": "openai/whisper-small", - "qwen": "Qwen/Qwen3-1.7B", - "lfm": "LiquidAI/LFM2-VL-450M", -} - - -def resolve_model_id_alias(model_id: str) -> str: - normalized = (model_id or "").strip() - return MODEL_ID_ALIASES.get(normalized.lower(), normalized) - - -def _python_runtime_library_path() -> Path: - suffix = ".dylib" if platform.system() == "Darwin" else ".so" - return PROJECT_ROOT / "cactus" / "build" / f"libcactus{suffix}" - - -def _static_cactus_library_path() -> Path: - return PROJECT_ROOT / "cactus" / "build" / "libcactus.a" - - -def _build_static_cactus_library() -> Path: - build_script = PROJECT_ROOT / "cactus" / "build.sh" - if not build_script.exists(): - raise RuntimeError(f"The Cactus build script is missing: {build_script}") - - build = subprocess.run([str(build_script)], cwd=PROJECT_ROOT / "cactus") - if build.returncode != 0: - raise RuntimeError("Failed to build the Cactus static runtime") - - static_library_path = _static_cactus_library_path() - if not static_library_path.exists(): - raise RuntimeError( - "The Cactus build completed, but the static library was not produced.\n" - f"Expected: {static_library_path}" - ) - return static_library_path - - -def _public_cactus_api_symbols(static_library_path: Path) -> list[str]: - if platform.system() == "Darwin": - command = ["nm", "-gU", str(static_library_path)] - else: - command = ["nm", "-g", "--defined-only", str(static_library_path)] - - result = subprocess.run(command, capture_output=True, text=True) - if result.returncode != 0: - raise RuntimeError( - "Failed to inspect the Cactus static runtime symbols.\n" - f"Command: {' '.join(command)}\n" - f"{result.stderr.strip()}" - ) - - symbols: list[str] = [] - for line in result.stdout.splitlines(): - parts = line.split() - if not parts: - continue - symbol = parts[-1].strip() - normalized = symbol[1:] if symbol.startswith("_") else symbol - if normalized.startswith("cactus_") and symbol not in symbols: - symbols.append(symbol) - if not symbols: - raise RuntimeError(f"Could not find any public cactus_* symbols in {static_library_path}") - return symbols - - -def _link_python_runtime_library(*, static_library_path: Path, library_path: Path) -> None: - build_dir = library_path.parent - build_dir.mkdir(parents=True, exist_ok=True) - if library_path.exists(): - library_path.unlink() - - exported_symbols = _public_cactus_api_symbols(static_library_path) - if platform.system() == "Darwin": - compiler = shutil.which("clang++") or shutil.which("c++") - if not compiler: - raise RuntimeError("Failed to find a C++ compiler for linking libcactus.dylib") - command = [ - compiler, - "-dynamiclib", - "-o", - str(library_path), - *[f"-Wl,-u,{symbol}" for symbol in exported_symbols], - str(static_library_path), - "-Wl,-install_name,@rpath/libcactus.dylib", - "-lcurl", - "-framework", - "Accelerate", - "-framework", - "CoreML", - "-framework", - "Foundation", - "-framework", - "Security", - "-framework", - "SystemConfiguration", - "-framework", - "CFNetwork", - ] - else: - compiler = shutil.which("g++") or shutil.which("c++") - if not compiler: - raise RuntimeError("Failed to find a C++ compiler for linking libcactus.so") - command = [ - compiler, - "-shared", - "-o", - str(library_path), - *[f"-Wl,--undefined={symbol}" for symbol in exported_symbols], - str(static_library_path), - "-lcurl", - "-pthread", - "-ldl", - "-lm", - ] - - result = subprocess.run(command, cwd=build_dir) - if result.returncode != 0 or not library_path.exists(): - raise RuntimeError(f"Failed to link the Cactus shared runtime: {library_path}") - - -def _ensure_python_runtime_library() -> Path: - library_path = _python_runtime_library_path() - static_library_path = _static_cactus_library_path() - if ( - library_path.exists() - and static_library_path.exists() - and library_path.stat().st_mtime >= static_library_path.stat().st_mtime - ): - return library_path - - print_color(YELLOW, "Preparing Cactus shared runtime for transpiler...") - if not static_library_path.exists(): - static_library_path = _build_static_cactus_library() - _link_python_runtime_library(static_library_path=static_library_path, library_path=library_path) - return library_path - - -def _weights_dir_looks_transpile_ready(weights_dir: Path) -> bool: +def _weights_dir_looks_transpile_ready(weights_dir): root = Path(weights_dir).expanduser().resolve() if not root.exists() or not root.is_dir(): return False @@ -168,32 +18,30 @@ def _weights_dir_looks_transpile_ready(weights_dir: Path) -> bool: return True if any(root.glob("*.cq[1-4].weights")): return True - # Legacy converted Cactus weight folders may not have a manifest yet, but - # they should still carry runtime config plus Cactus tensor files. return (root / "config.txt").exists() and any(root.glob("*.weights")) -def _extra_args_has_option(extra_args: list[str], option: str) -> bool: +def _extra_args_has_option(extra_args, option): prefix = f"{option}=" return any(arg == option or arg.startswith(prefix) for arg in extra_args) -def _prepend_python_path(env: dict[str, str]) -> None: +def _prepend_python_path(env): python_root = str(PROJECT_ROOT / "python") existing = env.get("PYTHONPATH") env["PYTHONPATH"] = python_root if not existing else f"{python_root}{os.pathsep}{existing}" -def cmd_transpile(args) -> int: +def cmd_transpile(args): """Invoke the saved-model transpiler entrypoint.""" - model_id = resolve_model_id_alias(args.model_id) + from .runtime import ensure_python_runtime_library + + model_id = resolve_model_id(args.model_id) extra_args = list(getattr(args, "extra_args", []) or []) allow_unconverted = bool(getattr(args, "allow_unconverted_weights", False)) command = [sys.executable, "-m", "cactus.transpile.hf_model", "--model-id", model_id] - if _extra_args_has_option(extra_args, "--weights-dir"): - pass - else: + if not _extra_args_has_option(extra_args, "--weights-dir"): default_weights_dir = get_weights_dir(model_id) if _weights_dir_looks_transpile_ready(default_weights_dir): command.extend(["--weights-dir", str(default_weights_dir)]) @@ -217,7 +65,7 @@ def cmd_transpile(args) -> int: command.extend(extra_args) try: - transpile_lib = _ensure_python_runtime_library() + transpile_lib = ensure_python_runtime_library() except RuntimeError as exc: print_color(RED, f"Error: {exc}") return 1 @@ -225,5 +73,4 @@ def cmd_transpile(args) -> int: env = os.environ.copy() env["CACTUS_LIB_PATH"] = str(transpile_lib) _prepend_python_path(env) - result = subprocess.run(command, cwd=PROJECT_ROOT, env=env) - return result.returncode + return subprocess.run(command, cwd=PROJECT_ROOT, env=env).returncode diff --git a/python/cactus/cli/utils.py b/python/cactus/cli/utils.py new file mode 100644 index 000000000..58b0da187 --- /dev/null +++ b/python/cactus/cli/utils.py @@ -0,0 +1,399 @@ +"""CQ archive discovery, download, and validation utilities.""" +from __future__ import annotations + +import hashlib +import json +import os +import re +import shutil +import tarfile +import tempfile +import urllib.parse +import urllib.request +import zipfile +from dataclasses import dataclass +from pathlib import Path +from typing import Iterable + + +MODALITIES = ("L", "V", "A") +COMBO_RE = re.compile(r"(?:[LVA][1-4])+", re.IGNORECASE) +COMBO_PART_RE = re.compile(r"([LVA])([1-4])", re.IGNORECASE) +ARCHIVE_SUFFIXES = (".zip", ".tar", ".tar.gz", ".tgz") + + +@dataclass(frozen=True) +class CqArchive: + filename: str + combo: dict[str, int] + size: int | None = None + sha256: str | None = None + + +@dataclass(frozen=True) +class CqResolution: + repo_id: str + local_name: str + archive: CqArchive + requested: dict[str, int] + warnings: tuple[str, ...] + available: tuple[CqArchive, ...] + + +def normalize_model_key(model_id: str) -> str: + return str(model_id).strip().lower().replace("_", "-") + + +def default_local_name(model_id: str) -> str: + name = str(model_id).strip().split("/")[-1].lower().replace("_", "-") + return name.removesuffix("-cq") + + +def suggested_cq_repo(model_id: str) -> str: + raw = str(model_id).strip().replace("_", "-") + name = raw.split("/")[-1] + if not name.lower().endswith("-cq"): + name = f"{name}-cq" + return f"Cactus-Compute/{name}" + + +def resolve_cq_repo(model_id: str) -> tuple[str, str]: + raw = str(model_id).strip() + key = normalize_model_key(raw) + + if "/" in raw and key.startswith("cactus-compute/") and key.endswith("-cq"): + return raw, default_local_name(raw) + + raise RuntimeError( + "download-cq expects a Cactus-Compute CQ repo ending in -cq. " + f"If you meant to download CQ weights for {raw}, try {suggested_cq_repo(raw)}." + ) + + +def archive_stem(filename: str) -> str: + for suffix in ARCHIVE_SUFFIXES: + if filename.lower().endswith(suffix): + return filename[: -len(suffix)] + return filename + + +def parse_combo(filename: str) -> dict[str, int]: + stem = archive_stem(Path(filename).name) + if not COMBO_RE.fullmatch(stem): + return {} + combo: dict[str, int] = {} + seen = set() + for modality, bits in COMBO_PART_RE.findall(stem): + modality = modality.upper() + if modality in seen: + return {} + seen.add(modality) + combo[modality] = int(bits) + return combo + + +def is_supported_archive(filename: str) -> bool: + lower = filename.lower() + return any(lower.endswith(suffix) for suffix in ARCHIVE_SUFFIXES) + + +def archives_from_repo_files(repo_files: Iterable[str], sizes: dict[str, int] | None = None, + sha256s: dict[str, str] | None = None) -> tuple[CqArchive, ...]: + sizes = sizes or {} + sha256s = sha256s or {} + archives = [] + for filename in repo_files: + if not is_supported_archive(filename): + continue + combo = parse_combo(filename) + if "L" not in combo: + continue + archives.append(CqArchive(filename=filename, combo=combo, + size=sizes.get(filename), sha256=sha256s.get(filename))) + return tuple(sorted(archives, key=lambda a: a.filename)) + + +def combo_label(combo: dict[str, int]) -> str: + return "".join(f"{m}{combo[m]}" for m in MODALITIES if m in combo) + + +def resolve_archive(repo_id: str, local_name: str, archives: Iterable[CqArchive], + requested: dict[str, int]) -> CqResolution: + available = tuple(archives) + if not available: + raise RuntimeError(f"No CQ archives found in {repo_id}") + + requested = {k.upper(): int(v) for k, v in requested.items() if v is not None} + repo_modalities = {m for archive in available for m in archive.combo} + effective = {} + warnings = [] + for modality in MODALITIES: + if modality not in requested: + continue + if modality not in repo_modalities: + warnings.append( + f"Requested {modality}{requested[modality]}, but {repo_id} has no {modality} modality; ignoring it." + ) + continue + effective[modality] = requested[modality] + + matches = [ + archive for archive in available + if all(archive.combo.get(modality) == bits for modality, bits in effective.items()) + ] + if not matches: + wanted = combo_label(effective) or combo_label(requested) + choices = ", ".join(combo_label(a.combo) for a in available) + raise RuntimeError(f"CQ combo {wanted} not found in {repo_id}. Available combos: {choices}") + + def score(archive: CqArchive) -> tuple[int, int, str]: + # Prefer archives that do not add unrequested modalities when possible, + # then prefer higher precision for unspecified modalities. + extra_modalities = len(set(archive.combo) - set(effective)) + precision_sum = sum(archive.combo.values()) + return (extra_modalities, -precision_sum, archive.filename) + + return CqResolution( + repo_id=repo_id, + local_name=local_name, + archive=sorted(matches, key=score)[0], + requested=requested, + warnings=tuple(warnings), + available=available, + ) + + +def _safe_zip_extract(zip_path: Path, out_dir: Path) -> None: + out_dir = out_dir.resolve() + with zipfile.ZipFile(zip_path, "r") as zf: + for info in zf.infolist(): + target = (out_dir / info.filename).resolve() + if target != out_dir and out_dir not in target.parents: + raise RuntimeError(f"Unsafe path in zip archive: {info.filename}") + mode = (info.external_attr >> 16) & 0o170000 + if mode in (0o120000, 0o10000): + raise RuntimeError(f"Refusing unsafe archive member: {info.filename}") + zf.extractall(out_dir) + + +def _safe_tar_extract(tar_path: Path, out_dir: Path) -> None: + out_dir = out_dir.resolve() + with tarfile.open(tar_path, "r:*") as tf: + for member in tf.getmembers(): + target = (out_dir / member.name).resolve() + if target != out_dir and out_dir not in target.parents: + raise RuntimeError(f"Unsafe path in tar archive: {member.name}") + if member.issym() or member.islnk(): + raise RuntimeError(f"Refusing link in tar archive: {member.name}") + tf.extractall(out_dir) + + +def safe_extract_archive(archive_path: Path, out_dir: Path) -> None: + lower = archive_path.name.lower() + if lower.endswith(".zip"): + _safe_zip_extract(archive_path, out_dir) + elif lower.endswith((".tar", ".tar.gz", ".tgz")): + _safe_tar_extract(archive_path, out_dir) + else: + raise RuntimeError(f"Unsupported CQ archive format: {archive_path.name}") + + +def promote_single_root(output_dir: Path) -> None: + if (output_dir / "config.txt").exists(): + return + + children = [path for path in output_dir.iterdir() if path.name != "__MACOSX"] + if len(children) != 1 or not children[0].is_dir(): + raise RuntimeError("CQ archive must contain config.txt at the archive root or under one top-level directory") + + nested = children[0] + if not (nested / "config.txt").exists(): + raise RuntimeError("CQ archive has one top-level directory, but it does not contain config.txt") + + for child in nested.iterdir(): + target = output_dir / child.name + if target.exists(): + raise RuntimeError(f"Cannot promote archive member over existing path: {target}") + child.rename(target) + nested.rmdir() + + +def validate_extracted_cq(output_dir: Path) -> None: + required = ("config.txt", "token_embeddings.weights", "vocab.txt", "tokenizer_config.txt") + missing = [name for name in required if not (output_dir / name).exists()] + if missing: + raise RuntimeError(f"Downloaded CQ package is missing required file(s): {', '.join(missing)}") + + tokenizer_config = {} + for line in (output_dir / "tokenizer_config.txt").read_text(encoding="utf-8").splitlines(): + line = line.strip() + if not line or line.startswith("#") or "=" not in line: + continue + key, value = line.split("=", 1) + tokenizer_config[key.strip()] = value.strip() + + tokenizer_type = tokenizer_config.get("tokenizer_type", "").lower() + optional_required = ["special_tokens.json", "tokenizer.json"] + if tokenizer_type == "bpe": + optional_required.append("merges.txt") + + missing_sidecars = [name for name in optional_required if not (output_dir / name).exists()] + if missing_sidecars: + raise RuntimeError(f"Downloaded CQ package is missing tokenizer sidecar file(s): {', '.join(missing_sidecars)}") + + +def sha256_file(path: Path) -> str: + h = hashlib.sha256() + with path.open("rb") as f: + for chunk in iter(lambda: f.read(1024 * 1024), b""): + h.update(chunk) + return h.hexdigest() + + +def verify_archive_sha256(archive_path: Path, expected_sha256: str | None) -> None: + if not expected_sha256: + return + actual = sha256_file(archive_path) + if actual.lower() != expected_sha256.lower(): + raise RuntimeError( + f"Downloaded archive checksum mismatch for {archive_path.name}: " + f"expected {expected_sha256}, got {actual}" + ) + + +def _download_with_urllib(resolution: CqResolution, *, token=None, cache_dir=None, revision=None) -> Path: + quoted_repo = urllib.parse.quote(resolution.repo_id, safe="") + quoted_file = urllib.parse.quote(resolution.archive.filename) + revision = revision or "main" + url = f"https://huggingface.co/{resolution.repo_id}/resolve/{urllib.parse.quote(str(revision), safe='')}/{quoted_file}" + + cache_root = Path(cache_dir).expanduser() if cache_dir else Path(tempfile.gettempdir()) / "cactus-cq-cache" + archive_dir = cache_root / quoted_repo / str(revision) + archive_dir.mkdir(parents=True, exist_ok=True) + archive_path = archive_dir / Path(resolution.archive.filename).name + if archive_path.exists() and ( + resolution.archive.size is None or archive_path.stat().st_size == resolution.archive.size + ): + return archive_path + + headers = {"User-Agent": "cactus-cq-downloader"} + if token: + headers["Authorization"] = f"Bearer {token}" + request = urllib.request.Request(url, headers=headers) + tmp_path = archive_path.with_suffix(archive_path.suffix + ".part") + with urllib.request.urlopen(request, timeout=60) as response, tmp_path.open("wb") as out: + shutil.copyfileobj(response, out, length=1024 * 1024) + tmp_path.rename(archive_path) + return archive_path + + +def write_download_metadata(output_dir: Path, resolution: CqResolution, archive_path: Path) -> None: + metadata = { + "repo_id": resolution.repo_id, + "local_name": resolution.local_name, + "archive": resolution.archive.filename, + "combo": resolution.archive.combo, + "requested": resolution.requested, + "warnings": list(resolution.warnings), + "archive_size": resolution.archive.size if resolution.archive.size is not None else archive_path.stat().st_size, + "archive_sha256": resolution.archive.sha256, + } + if metadata["archive_sha256"] is None and os.getenv("CACTUS_CQ_HASH_DOWNLOAD", "") == "1": + metadata["archive_sha256"] = sha256_file(archive_path) + (output_dir / ".cactus_cq_download.json").write_text(json.dumps(metadata, indent=2) + "\n", encoding="utf-8") + + +def download_cq_archive(resolution: CqResolution, output_dir: Path, *, token=None, + cache_dir=None, revision=None, force=False, dry_run=False) -> Path: + if dry_run: + return output_dir + + if output_dir.exists() and force: + shutil.rmtree(output_dir) + if output_dir.exists() and (output_dir / "config.txt").exists(): + return output_dir + if output_dir.exists() and any(output_dir.iterdir()): + raise RuntimeError(f"Output directory already exists and is not a complete CQ package: {output_dir}") + + try: + from huggingface_hub import hf_hub_download + archive_path = Path(hf_hub_download( + repo_id=resolution.repo_id, + filename=resolution.archive.filename, + repo_type="model", + token=token, + cache_dir=cache_dir, + revision=revision, + )) + except ImportError: + archive_path = _download_with_urllib( + resolution, + token=token, + cache_dir=cache_dir, + revision=revision, + ) + verify_archive_sha256(archive_path, resolution.archive.sha256) + + output_dir.parent.mkdir(parents=True, exist_ok=True) + with tempfile.TemporaryDirectory(prefix=f".{output_dir.name}.", dir=str(output_dir.parent)) as tmp: + tmp_dir = Path(tmp) + safe_extract_archive(archive_path, tmp_dir) + promote_single_root(tmp_dir) + validate_extracted_cq(tmp_dir) + write_download_metadata(tmp_dir, resolution, archive_path) + if output_dir.exists(): + shutil.rmtree(output_dir) + tmp_dir.rename(output_dir) + + return output_dir + + +def list_hf_cq_archives(repo_id: str, *, token=None, revision=None) -> tuple[CqArchive, ...]: + try: + from huggingface_hub import HfApi + except ImportError: + quoted = urllib.parse.quote(repo_id, safe="/") + url = f"https://huggingface.co/api/models/{quoted}?blobs=true" + if revision: + url += f"&revision={urllib.parse.quote(str(revision), safe='')}" + headers = {"User-Agent": "cactus-cq-downloader"} + if token: + headers["Authorization"] = f"Bearer {token}" + req = urllib.request.Request(url, headers=headers) + with urllib.request.urlopen(req, timeout=30) as response: + data = json.load(response) + files = [] + sizes = {} + sha256s = {} + for sibling in data.get("siblings", []): + filename = sibling.get("rfilename") + if not filename: + continue + files.append(filename) + lfs = sibling.get("lfs") or {} + if "size" in lfs: + sizes[filename] = int(lfs["size"]) + elif "size" in sibling: + sizes[filename] = int(sibling["size"]) + if "sha256" in lfs: + sha256s[filename] = lfs["sha256"] + return archives_from_repo_files(files, sizes=sizes, sha256s=sha256s) + + info = HfApi().model_info(repo_id, revision=revision, token=token, files_metadata=True) + files = [] + sizes = {} + sha256s = {} + for sibling in info.siblings: + filename = sibling.rfilename + files.append(filename) + size = getattr(sibling, "size", None) + if size is not None: + sizes[filename] = int(size) + lfs = getattr(sibling, "lfs", None) + if isinstance(lfs, dict): + if "sha256" in lfs: + sha256s[filename] = lfs["sha256"] + if "size" in lfs: + sizes[filename] = int(lfs["size"]) + return archives_from_repo_files(files, sizes=sizes, sha256s=sha256s) + diff --git a/python/cactus/py.typed b/python/cactus/py.typed new file mode 100644 index 000000000..e69de29bb diff --git a/python/cactus/transpile/component_plan.py b/python/cactus/transpile/component_plan.py index 0357fefb0..31f88ebef 100644 --- a/python/cactus/transpile/component_plan.py +++ b/python/cactus/transpile/component_plan.py @@ -19,9 +19,20 @@ class ComponentPlan: force_component_pipeline: bool = False -def _plan_from_profile(profile: ModelProfile) -> ComponentPlan | None: +def _plan_from_profile( + profile: ModelProfile, + config: Mapping[str, object] | None = None, +) -> ComponentPlan | None: if profile.default_task is None or not profile.default_components: return None + if config is not None and profile.needs_image: + has_vision = _has_dict_config(config, "vision_config", "visual_config", "image_config") + if not has_vision: + return ComponentPlan( + task="causal_lm_logits", + components=("decoder",) + tuple(profile.cached_step_components or ()), + force_component_pipeline=bool(profile.cached_step_components), + ) return ComponentPlan( task=profile.default_task, components=profile.default_components, @@ -131,7 +142,7 @@ def infer_component_plan_from_config( lowered_id = model_id.lower() architectures = _lowered_architectures(config, config_txt) profile = profile_for_model_type(model_type) or profile_for_model_id(model_id) - profile_plan = _plan_from_profile(profile) if profile is not None else None + profile_plan = _plan_from_profile(profile, config) if profile is not None else None if profile_plan is not None: return profile_plan diff --git a/python/cactus/transpile/model_adapters.py b/python/cactus/transpile/model_adapters.py index 62be8a529..42e91222c 100644 --- a/python/cactus/transpile/model_adapters.py +++ b/python/cactus/transpile/model_adapters.py @@ -839,8 +839,8 @@ def _gemma4_compute_native_like_image_features( raise TypeError("Gemma4 vision embedder is missing embedding_projection") projected = F.linear( pooled_hidden, - projection.weight, - projection.bias, + projection.weight.float(), + None if projection.bias is None else projection.bias.float(), ) if post_proj_norm_weight is not None and post_proj_norm_weight.numel() > 0: return _gemma4_rms_norm( diff --git a/python/pyproject.toml b/python/pyproject.toml index d63e78139..662328149 100644 --- a/python/pyproject.toml +++ b/python/pyproject.toml @@ -1,25 +1,66 @@ [project] -name = "cactus" -version = "1.14.0" -description = "Cactus Python tools" +name = "cactus-compute" +dynamic = ["version"] +description = "On-device AI inference — LLM, vision, speech, embeddings, and RAG" +readme = "README.md" requires-python = ">=3.10" +license = {text = "Cactus Compute License"} +authors = [{name = "Cactus Compute"}] +keywords = [ + "ai", "inference", "on-device", "llm", "embeddings", + "transcription", "rag", "quantization", "edge-ai", +] +classifiers = [ + "Development Status :: 4 - Beta", + "Environment :: Console", + "Intended Audience :: Developers", + "Intended Audience :: Science/Research", + "License :: Other/Proprietary License", + "Operating System :: MacOS", + "Operating System :: POSIX :: Linux", + "Programming Language :: Python :: 3", + "Programming Language :: Python :: 3.10", + "Programming Language :: Python :: 3.11", + "Programming Language :: Python :: 3.12", + "Programming Language :: Python :: 3.13", + "Programming Language :: Python :: 3.14", + "Topic :: Multimedia :: Sound/Audio :: Speech", + "Topic :: Scientific/Engineering :: Artificial Intelligence", + "Topic :: Scientific/Engineering :: Image Recognition", +] dependencies = [ "torch>=2.8.0,<3", - "transformers>=5.5.4,<6", + "transformers==5.5.4", "numpy>=1.26.0,<3", "huggingface-hub>=1.5.0,<2.0", ] [project.optional-dependencies] vlm = ["Pillow>=11.0.0,<12", "torchvision>=0.23.0,<1"] +dev = ["pytest>=8.0", "scipy>=1.13"] + +[project.urls] +Homepage = "https://cactuscompute.com" +Repository = "https://github.com/cactus-compute/cactus" +Documentation = "https://docs.cactuscompute.com" +"Bug Tracker" = "https://github.com/cactus-compute/cactus/issues" [project.scripts] cactus = "cactus.cli:main" [build-system] -requires = ["setuptools>=61.0"] +requires = ["setuptools>=61.0", "wheel"] build-backend = "setuptools.build_meta" +[tool.setuptools.dynamic] +version = {attr = "cactus.__version__"} + [tool.setuptools.packages.find] where = ["."] include = ["cactus*"] + +[tool.setuptools.package-data] +"cactus.bindings" = ["lib/*.dylib", "lib/*.so"] + +[tool.pytest.ini_options] +testpaths = ["tests"] diff --git a/python/tests/test_bindings.py b/python/tests/test_bindings.py new file mode 100644 index 000000000..e907cb431 --- /dev/null +++ b/python/tests/test_bindings.py @@ -0,0 +1,515 @@ +"""Comprehensive tests for cactus Python bindings. + +Tests the helper functions, auto-serialization, edge cases, and package +structure. The actual C FFI calls require a compiled libcactus — those +are integration tests run via `cactus test`. +""" +import json +import ctypes +import sys +from pathlib import Path + +import pytest + +sys.path.insert(0, str(Path(__file__).parent.parent)) + + +# ── Import and structure tests ────────────────────────────────────── + + +class TestPackageStructure: + """Verify the package is importable and has the right public API.""" + + def test_version_exists(self): + from cactus import __version__ + assert __version__ == "2.0.0" + + def test_cli_module_importable(self): + from cactus.cli import main, create_parser + assert callable(main) + assert callable(create_parser) + + def test_model_module_importable(self): + from cactus.cli.model import resolve_model_id, ensure_weights, ensure_bundle, TranspileOptions + assert callable(resolve_model_id) + assert callable(ensure_weights) + assert callable(ensure_bundle) + + def test_public_api_reexports(self): + from cactus import ensure_model, get_weights_dir, get_model_dir_name + assert callable(ensure_model) + assert callable(get_weights_dir) + assert callable(get_model_dir_name) + + def test_py_typed_marker_exists(self): + marker = Path(__file__).parent.parent / "cactus" / "py.typed" + assert marker.exists(), "py.typed PEP 561 marker missing" + + +# ── Helper function tests ─────────────────────────────────────────── + + +class TestToJson: + """Test the _to_json auto-serialization helper.""" + + def setup_method(self): + from cactus.bindings.cactus import _to_json + self.to_json = _to_json + + def test_none_passthrough(self): + assert self.to_json(None) is None + + def test_dict_serialized(self): + result = self.to_json({"temperature": 0.7, "max_tokens": 100}) + assert isinstance(result, bytes) + parsed = json.loads(result) + assert parsed == {"temperature": 0.7, "max_tokens": 100} + + def test_list_serialized(self): + messages = [{"role": "user", "content": "Hello"}] + result = self.to_json(messages) + assert isinstance(result, bytes) + parsed = json.loads(result) + assert parsed == messages + + def test_string_encoded(self): + result = self.to_json('{"already": "json"}') + assert isinstance(result, bytes) + assert result == b'{"already": "json"}' + + def test_bytes_passthrough(self): + raw = b'{"raw": "bytes"}' + result = self.to_json(raw) + assert result is raw + + def test_empty_dict(self): + result = self.to_json({}) + assert json.loads(result) == {} + + def test_empty_list(self): + result = self.to_json([]) + assert json.loads(result) == [] + + def test_nested_structure(self): + data = {"messages": [{"role": "user", "content": "test"}], "options": {"temp": 0.5}} + result = self.to_json(data) + assert json.loads(result) == data + + def test_unicode_content(self): + data = [{"role": "user", "content": "Привет мир 你好世界"}] + result = self.to_json(data) + parsed = json.loads(result) + assert parsed[0]["content"] == "Привет мир 你好世界" + + +class TestFromJson: + """Test the _from_json buffer decoder helper.""" + + def setup_method(self): + from cactus.bindings.cactus import _from_json + self.from_json = _from_json + + def _make_buf(self, text): + encoded = text.encode() if isinstance(text, str) else text + buf = ctypes.create_string_buffer(len(encoded) + 1) + buf.value = encoded + return buf + + def test_valid_json(self): + result = self.from_json(self._make_buf('{"key": "value"}')) + assert result == {"key": "value"} + + def test_empty_buffer_returns_empty_dict(self): + buf = ctypes.create_string_buffer(64) + result = self.from_json(buf) + assert result == {} + + def test_array_json(self): + result = self.from_json(self._make_buf('[1, 2, 3]')) + assert result == [1, 2, 3] + + def test_numeric_values(self): + result = self.from_json(self._make_buf('{"score": 0.95, "count": 42}')) + assert result["score"] == 0.95 + assert result["count"] == 42 + + def test_nested_json(self): + text = '{"response": "Hi", "metrics": {"tokens": 5, "latency_ms": 123.4}}' + result = self.from_json(self._make_buf(text)) + assert result["response"] == "Hi" + assert result["metrics"]["tokens"] == 5 + + def test_unicode_response(self): + result = self.from_json(self._make_buf('{"text": "日本語テスト"}')) + assert result["text"] == "日本語テスト" + + +class TestPreparePcm: + """Test the _prepare_pcm audio marshaling helper.""" + + def setup_method(self): + from cactus.bindings.cactus import _prepare_pcm + self.prepare_pcm = _prepare_pcm + + def test_none_returns_null(self): + ptr, size = self.prepare_pcm(None) + assert ptr is None + assert size == 0 + + def test_bytes_marshaled(self): + data = b"\x00\x01\x02\x03\xff" + ptr, size = self.prepare_pcm(data) + assert ptr is not None + assert size == 5 + + def test_empty_bytes(self): + ptr, size = self.prepare_pcm(b"") + assert size == 0 + + def test_large_buffer(self): + data = bytes(range(256)) * 100 # 25600 bytes + ptr, size = self.prepare_pcm(data) + assert size == 25600 + + +class TestMakeTokenCallback: + """Test the callback wrapper helper.""" + + def setup_method(self): + from cactus.bindings.cactus import _make_token_callback + self.make_cb = _make_token_callback + + def test_none_callback(self): + cb = self.make_cb(None) + assert cb is not None # Returns empty TokenCallback + + def test_false_callback(self): + cb = self.make_cb(False) + assert cb is not None + + def test_callable_wrapped(self): + tokens = [] + def my_callback(text, token_id): + tokens.append((text, token_id)) + cb = self.make_cb(my_callback) + assert cb is not None + + +class TestEnc: + """Test the _enc string encoding helper.""" + + def setup_method(self): + from cactus.bindings.cactus import _enc + self.enc = _enc + + def test_none_passthrough(self): + assert self.enc(None) is None + + def test_string_encoded(self): + result = self.enc("hello") + assert result == b"hello" + + def test_bytes_passthrough(self): + raw = b"hello" + result = self.enc(raw) + assert result is raw + + def test_unicode_encoded(self): + result = self.enc("日本語") + assert result == "日本語".encode() + + def test_empty_string(self): + result = self.enc("") + assert result == b"" + + +# ── Model ID resolution tests ────────────────────────────────────── + + +class TestResolveModelId: + """Test model alias resolution.""" + + def setup_method(self): + from cactus.cli.model import resolve_model_id + self.resolve = resolve_model_id + + def test_known_aliases(self): + assert self.resolve("gemma4") == "google/gemma-4-E2B-it" + assert self.resolve("parakeet") == "nvidia/parakeet-tdt-0.6b-v3" + assert self.resolve("whisper") == "openai/whisper-small" + assert self.resolve("qwen") == "Qwen/Qwen3-1.7B" + assert self.resolve("lfm") == "LiquidAI/LFM2-VL-450M" + + def test_case_insensitive(self): + assert self.resolve("GEMMA4") == "google/gemma-4-E2B-it" + assert self.resolve("Parakeet") == "nvidia/parakeet-tdt-0.6b-v3" + + def test_unknown_passthrough(self): + assert self.resolve("meta/llama-3.3-70b") == "meta/llama-3.3-70b" + + def test_whitespace_stripped(self): + assert self.resolve(" gemma4 ") == "google/gemma-4-E2B-it" + + def test_empty_string(self): + assert self.resolve("") == "" + + def test_none_safe(self): + assert self.resolve(None) == "" + + +# ── TranspileOptions tests ────────────────────────────────────────── + + +class TestTranspileOptions: + """Test the TranspileOptions dataclass.""" + + def test_defaults(self): + from cactus.cli.model import TranspileOptions + opts = TranspileOptions() + assert opts.task == "auto" + assert opts.prompt is None + assert opts.image_files is None + assert opts.audio_file is None + assert opts.max_new_tokens is None + assert opts.component_pipeline == "auto" + assert opts.components is None + assert opts.system_prompt is None + assert opts.trust_remote_code is False + assert opts.local_files_only is False + + def test_custom_values(self): + from cactus.cli.model import TranspileOptions + opts = TranspileOptions( + task="causal_lm_logits", + prompt="Hello", + max_new_tokens=256, + trust_remote_code=True, + ) + assert opts.task == "causal_lm_logits" + assert opts.prompt == "Hello" + assert opts.max_new_tokens == 256 + assert opts.trust_remote_code is True + + def test_frozen(self): + from cactus.cli.model import TranspileOptions + opts = TranspileOptions() + with pytest.raises(AttributeError): + opts.task = "something" + + +# ── CLI parser tests ──────────────────────────────────────────────── + + +class TestCliParser: + """Test CLI argument parsing.""" + + def setup_method(self): + from cactus.cli import create_parser + self.parser = create_parser() + + def test_download_command(self): + args = self.parser.parse_args(["download", "google/gemma-4-E2B-it"]) + assert args.command == "download" + assert args.model_id == "google/gemma-4-E2B-it" + + def test_download_defaults(self): + args = self.parser.parse_args(["download"]) + assert args.token is None + assert args.cache_dir is None + + def test_run_command(self): + args = self.parser.parse_args(["run", "gemma4", "--prompt", "hi"]) + assert args.command == "run" + assert args.model_id == "gemma4" + assert args.prompt == "hi" + + def test_convert_command(self): + args = self.parser.parse_args(["convert", "qwen", "--bits", "2"]) + assert args.command == "convert" + assert args.model_name == "qwen" + assert args.bits == 2 + + def test_build_command(self): + args = self.parser.parse_args(["build", "--apple"]) + assert args.command == "build" + assert args.apple is True + + def test_test_command_suite(self): + args = self.parser.parse_args(["test", "--suite", "llm"]) + assert args.command == "test" + assert args.suite == "llm" + + def test_test_command_default_suite(self): + args = self.parser.parse_args(["test"]) + assert args.command == "test" + assert args.suite is None + + def test_auth_command(self): + args = self.parser.parse_args(["auth", "--status"]) + assert args.command == "auth" + assert args.status is True + + def test_clean_command(self): + args = self.parser.parse_args(["clean"]) + assert args.command == "clean" + + def test_no_command_prints_help(self): + args = self.parser.parse_args([]) + assert args.command is None + + def test_transpile_not_exposed(self): + with pytest.raises(SystemExit): + self.parser.parse_args(["transpile", "gemma4"]) + + + +# ── Index API tests ──────────────────────────────────────────────── + + +class TestIndexApi: + """Test the vector index API (no model weights needed).""" + + def setup_method(self): + import tempfile + from cactus.bindings.cactus import ( + cactus_index_init, cactus_index_add, cactus_index_query, + cactus_index_get, cactus_index_delete, cactus_index_compact, + cactus_index_destroy, + ) + self.tmpdir = tempfile.mkdtemp() + self.init = cactus_index_init + self.add = cactus_index_add + self.query = cactus_index_query + self.get = cactus_index_get + self.delete = cactus_index_delete + self.compact = cactus_index_compact + self.destroy = cactus_index_destroy + self._handles = [] + + def _create_index(self, dim): + idx = self.init(self.tmpdir, dim) + self._handles.append(idx) + return idx + + def teardown_method(self): + import shutil + for h in self._handles: + try: + self.destroy(h) + except Exception: + pass + self._handles.clear() + shutil.rmtree(self.tmpdir, ignore_errors=True) + + def test_init_destroy(self): + idx = self._create_index(4) + assert idx is not None + + def test_add_and_query(self): + idx = self._create_index(4) + self.add(idx, [0, 1, 2], ["doc a", "doc b", "doc c"], + embeddings=[[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 1, 0]]) + result = self.query(idx, [1, 0, 0, 0], {"n_results": 2}) + ids = [r["id"] for r in result["results"]] + assert 0 in ids + + def test_get_by_id(self): + idx = self._create_index(3) + self.add(idx, [42], ["hello world"], embeddings=[[1, 0, 0]]) + result = self.get(idx, [42]) + assert result["results"][0]["document"] == "hello world" + + def test_delete(self): + idx = self._create_index(3) + self.add(idx, [0, 1], ["a", "b"], embeddings=[[1, 0, 0], [0, 1, 0]]) + self.delete(idx, [0]) + result = self.query(idx, [1, 0, 0], {"n_results": 10}) + ids = [r["id"] for r in result["results"]] + assert 0 not in ids + + def test_compact(self): + idx = self._create_index(3) + self.add(idx, [0], ["x"], embeddings=[[1, 0, 0]]) + self.compact(idx) + result = self.query(idx, [1, 0, 0], {"n_results": 1}) + assert len(result["results"]) >= 1 + + def test_persistence(self): + idx = self._create_index(3) + self.add(idx, [0], ["persistent"], embeddings=[[1, 0, 0]]) + self.destroy(idx) + self._handles.remove(idx) + idx2 = self._create_index(3) + result = self.get(idx2, [0]) + assert result["results"][0]["document"] == "persistent" + + +# ── Streaming callback tests ────────────────────────────────────── + + +class TestStreamingCallbacks: + """Test the token callback wrapper (no model weights needed).""" + + def setup_method(self): + from cactus.bindings.cactus import _make_token_callback, TokenCallback + self.make_cb = _make_token_callback + self.TokenCallback = TokenCallback + + def test_none_returns_valid_callback(self): + cb = self.make_cb(None) + assert cb is not None + + def test_false_returns_valid_callback(self): + cb = self.make_cb(False) + assert cb is not None + + def test_callable_returns_valid_callback(self): + cb = self.make_cb(lambda text, tid: None) + assert cb is not None + + def test_callback_is_correct_ctypes_type(self): + cb = self.make_cb(lambda text, tid: None) + assert isinstance(cb, self.TokenCallback) + + def test_multiple_callbacks_independent(self): + results_a = [] + results_b = [] + cb_a = self.make_cb(lambda t, i: results_a.append(t)) + cb_b = self.make_cb(lambda t, i: results_b.append(t)) + assert cb_a is not cb_b + + +# ── Error path tests ────────────────────────────────────────────── + + +class TestErrorPaths: + """Test error handling (no model weights needed).""" + + def test_init_bad_path_raises(self): + from cactus.bindings.cactus import cactus_init + with pytest.raises(RuntimeError): + cactus_init("/nonexistent/path/to/model") + + def test_init_empty_path_raises(self): + from cactus.bindings.cactus import cactus_init + with pytest.raises(RuntimeError): + cactus_init("") + + def test_get_last_error_returns_string(self): + from cactus.bindings.cactus import cactus_init, cactus_get_last_error + try: + cactus_init("/nonexistent") + except RuntimeError: + pass + err = cactus_get_last_error() + assert isinstance(err, str) + assert len(err) > 0 + + def test_init_bad_path_error_message(self): + from cactus.bindings.cactus import cactus_init, cactus_get_last_error + with pytest.raises(RuntimeError, match="config"): + cactus_init("/nonexistent/model") + + +if __name__ == "__main__": + pytest.main([__file__, "-v"]) diff --git a/python/tests/test_cli_transpile_defaults.py b/python/tests/test_cli_transpile_defaults.py index 676c2eeb2..5b1141dd0 100644 --- a/python/tests/test_cli_transpile_defaults.py +++ b/python/tests/test_cli_transpile_defaults.py @@ -5,6 +5,7 @@ from cactus import cli from cactus.cli import convert as convert_cli +import cactus.cli.model as model_mod def _write_gemma4_multimodal_config(output_dir: Path) -> None: @@ -21,7 +22,8 @@ def _write_gemma4_multimodal_config(output_dir: Path) -> None: def _gemma4_multimodal_extra_args(model_dir: Path, artifact_dir: Path) -> list[str]: - assets_dir = convert_cli.PROJECT_ROOT / "cactus-engine" / "tests" / "assets" + from cactus.cli.common import PROJECT_ROOT + assets_dir = PROJECT_ROOT / "cactus-engine" / "tests" / "assets" return [ "--weights-dir", str(model_dir), @@ -30,11 +32,11 @@ def _gemma4_multimodal_extra_args(model_dir: Path, artifact_dir: Path) -> list[s "--task", "multimodal_causal_lm_logits", "--max-new-tokens", - "32", + "512", "--component-pipeline", "on", "--prompt", - convert_cli._DEFAULT_MULTIMODAL_PROMPT, + model_mod._DEFAULT_MULTIMODAL_PROMPT, "--components", "vision_encoder,audio_encoder,lm_encoder,decoder", "--image-file", @@ -47,7 +49,7 @@ def _gemma4_multimodal_extra_args(model_dir: Path, artifact_dir: Path) -> list[s def test_cmd_convert_transpiles_into_same_weights_folder(monkeypatch, tmp_path: Path) -> None: parser = cli.create_parser() - args = parser.parse_args(["convert", "gemma4"]) + args = parser.parse_args(["convert", "gemma4", "--reconvert"]) model_dir = tmp_path / "weights" / "gemma-4-e2b-it" calls: list[tuple[str, object]] = [] @@ -65,10 +67,11 @@ def _fake_cmd_transpile(transpile_args): return 0 monkeypatch.setattr(convert_cli, "get_weights_dir", lambda model_id: model_dir) - monkeypatch.setattr(convert_cli, "cmd_transpile", _fake_cmd_transpile) - import cactus.convert.cli as cq_cli + import cactus.cli.transpile as transpile_mod + monkeypatch.setattr(transpile_mod, "cmd_transpile", _fake_cmd_transpile) + import cactus.convert.cli as cq_cli monkeypatch.setattr(cq_cli, "main", _fake_cq_main) rc = convert_cli.cmd_convert(args) @@ -93,7 +96,7 @@ def _fake_cmd_transpile(transpile_args): def test_cmd_convert_honors_explicit_output_dir(monkeypatch, tmp_path: Path) -> None: parser = cli.create_parser() output_dir = tmp_path / "custom" - args = parser.parse_args(["convert", "google/gemma-4-E2B-it", str(output_dir)]) + args = parser.parse_args(["convert", "google/gemma-4-E2B-it", str(output_dir), "--reconvert"]) cq_calls: list[list[str]] = [] @@ -108,10 +111,10 @@ def _fake_cmd_transpile(transpile_args): transpile_calls.append(transpile_args) return 0 - monkeypatch.setattr(convert_cli, "cmd_transpile", _fake_cmd_transpile) + import cactus.cli.transpile as transpile_mod + monkeypatch.setattr(transpile_mod, "cmd_transpile", _fake_cmd_transpile) import cactus.convert.cli as cq_cli - monkeypatch.setattr(cq_cli, "main", _fake_cq_main) rc = convert_cli.cmd_convert(args) @@ -150,10 +153,10 @@ def _fake_cmd_transpile(transpile_args): transpile_calls.append(transpile_args) return 0 - monkeypatch.setattr(convert_cli, "cmd_transpile", _fake_cmd_transpile) + import cactus.cli.transpile as transpile_mod + monkeypatch.setattr(transpile_mod, "cmd_transpile", _fake_cmd_transpile) import cactus.convert.cli as cq_cli - monkeypatch.setattr(cq_cli, "main", _fake_cq_main) rc = convert_cli.cmd_convert(args) @@ -184,10 +187,10 @@ def _fake_cmd_transpile(transpile_args): transpile_calls.append(transpile_args) return 0 - monkeypatch.setattr(convert_cli, "cmd_transpile", _fake_cmd_transpile) + import cactus.cli.transpile as transpile_mod + monkeypatch.setattr(transpile_mod, "cmd_transpile", _fake_cmd_transpile) import cactus.convert.cli as cq_cli - monkeypatch.setattr(cq_cli, "main", _fake_cq_main) rc = convert_cli.cmd_convert(args) @@ -198,17 +201,57 @@ def _fake_cmd_transpile(transpile_args): assert "--audio-file" in extra_args -def test_cmd_convert_infers_text_tasks_for_qwen_and_lfm(monkeypatch, tmp_path: Path) -> None: +def test_cmd_convert_infers_multimodal_for_qwen_and_lfm(monkeypatch, tmp_path: Path) -> None: + """Qwen/LFM models WITH vision_config get multimodal task.""" parser = cli.create_parser() import cactus.convert.cli as cq_cli + import cactus.cli.transpile as transpile_mod + + for alias, model_type, arch, vision in ( + ("qwen", "qwen3", "Qwen3ForCausalLM", "qwen3_vision"), + ("lfm", "lfm2_vl", "Lfm2VlForConditionalGeneration", "siglip2_vision_model"), + ): + output_dir = tmp_path / (alias + "_mm") + args = parser.parse_args(["convert", alias, str(output_dir), "--reconvert"]) + transpile_calls: list[Namespace] = [] + + def _fake_cq_main(command, *, output_dir=output_dir, model_type=model_type, arch=arch, vision=vision): + output_dir.mkdir(parents=True, exist_ok=True) + (output_dir / "hf_config.json").write_text( + f'{{"model_type":"{model_type}","architectures":["{arch}"],' + f'"vision_config":{{"model_type":"{vision}"}}}}', + encoding="utf-8", + ) + return 0 + + def _fake_cmd_transpile(transpile_args): + transpile_calls.append(transpile_args) + return 0 + + monkeypatch.setattr(transpile_mod, "cmd_transpile", _fake_cmd_transpile) + monkeypatch.setattr(cq_cli, "main", _fake_cq_main) + + rc = convert_cli.cmd_convert(args) + + assert rc == 0 + extra_args = transpile_calls[0].extra_args + assert extra_args[extra_args.index("--task") + 1] == "multimodal_causal_lm_logits" + + +def test_cmd_convert_infers_causal_lm_for_qwen_and_lfm_without_vision(monkeypatch, tmp_path: Path) -> None: + """Qwen/LFM models WITHOUT vision_config get causal_lm_logits.""" + parser = cli.create_parser() + + import cactus.convert.cli as cq_cli + import cactus.cli.transpile as transpile_mod for alias, model_type, arch in ( ("qwen", "qwen3", "Qwen3ForCausalLM"), ("lfm", "lfm2_vl", "Lfm2VlForConditionalGeneration"), ): output_dir = tmp_path / alias - args = parser.parse_args(["convert", alias, str(output_dir)]) + args = parser.parse_args(["convert", alias, str(output_dir), "--reconvert"]) transpile_calls: list[Namespace] = [] def _fake_cq_main(command, *, output_dir=output_dir, model_type=model_type, arch=arch): @@ -223,7 +266,7 @@ def _fake_cmd_transpile(transpile_args): transpile_calls.append(transpile_args) return 0 - monkeypatch.setattr(convert_cli, "cmd_transpile", _fake_cmd_transpile) + monkeypatch.setattr(transpile_mod, "cmd_transpile", _fake_cmd_transpile) monkeypatch.setattr(cq_cli, "main", _fake_cq_main) rc = convert_cli.cmd_convert(args) @@ -231,7 +274,6 @@ def _fake_cmd_transpile(transpile_args): assert rc == 0 extra_args = transpile_calls[0].extra_args assert extra_args[extra_args.index("--task") + 1] == "causal_lm_logits" - assert "--audio-file" not in extra_args def test_cmd_convert_infers_multimodal_components_from_vision_config(monkeypatch, tmp_path: Path) -> None: @@ -257,10 +299,10 @@ def _fake_cmd_transpile(transpile_args): transpile_calls.append(transpile_args) return 0 - monkeypatch.setattr(convert_cli, "cmd_transpile", _fake_cmd_transpile) + import cactus.cli.transpile as transpile_mod + monkeypatch.setattr(transpile_mod, "cmd_transpile", _fake_cmd_transpile) import cactus.convert.cli as cq_cli - monkeypatch.setattr(cq_cli, "main", _fake_cq_main) rc = convert_cli.cmd_convert(args) diff --git a/python/tests/test_download.py b/python/tests/test_download.py index 46155732a..6ea1257d5 100644 --- a/python/tests/test_download.py +++ b/python/tests/test_download.py @@ -8,7 +8,7 @@ sys.path.insert(0, str(Path(__file__).parent.parent)) -from cactus.cli.download import ( +from cactus.cli.utils import ( archives_from_repo_files, combo_label, parse_combo,