#!/usr/bin/env bash # Build the CPU-only laya.cpp runtime used by classify_runner.py. # # Safe to re-run: every step is skipped when its output already exists. # Nothing here needs sudo. cmake/ninja come from pip wheels and # nlohmann/json is unpacked into a local prefix, so the only system # requirement is libicu-dev (already present on Debian/Ubuntu). set -euo pipefail BASE_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" LAYACPP="$BASE_DIR/third_party/laya.cpp" PREFIX="$BASE_DIR/third_party/nlohmann-install" BUILD="$LAYACPP/build-cpu" PYTHON="${PYTHON:-$BASE_DIR/.venv/bin/python}" if [ ! -x "$PYTHON" ]; then echo "Venv not found. Create it with: python3 -m venv .venv && .venv/bin/pip install -r requirements.txt" >&2 exit 1 fi # cmake and ninja are build tools, not runtime deps, so they are installed here # rather than added to requirements.txt. Pinned below 4.x: laya.cpp's pinned ggml # declares cmake_minimum_required(VERSION 3.14...3.28) and does not configure on 4.x. if ! "$BASE_DIR/.venv/bin/cmake" --version 2>/dev/null | grep -qE '^cmake version 3\.'; then echo "==> Installing cmake (<4) and ninja" "$PYTHON" -m pip install --quiet "cmake==3.31.*" "ninja<2" fi export PATH="$BASE_DIR/.venv/bin:$PATH" if [ ! -d "$LAYACPP/.git" ]; then echo "==> Cloning laya.cpp" git clone --recursive --shallow-submodules https://github.com/lkarlslund/laya.cpp "$LAYACPP" fi # laya.cpp needs the nlohmann_json CMake package. Debian ships it as # nlohmann-json3-dev, but a local prefix avoids the sudo requirement. if [ ! -f "$PREFIX/share/cmake/nlohmann_json/nlohmann_jsonConfig.cmake" ]; then echo "==> Unpacking nlohmann/json" tarball="$(mktemp -t json-XXXXXX.tar.xz)" trap 'rm -f "$tarball"' EXIT curl -fsSL "https://github.com/nlohmann/json/releases/download/v3.11.3/json.tar.xz" -o "$tarball" mkdir -p "$BASE_DIR/third_party/nlohmann-prefix" tar xJf "$tarball" -C "$BASE_DIR/third_party/nlohmann-prefix" --strip-components=1 # JSON_BuildTests=OFF because the release tarball ships no tests/ directory. cmake -S "$BASE_DIR/third_party/nlohmann-prefix" -B "$BASE_DIR/third_party/nlohmann-build" -G Ninja \ -DCMAKE_BUILD_TYPE=Release -DJSON_BuildTests=OFF -DCMAKE_INSTALL_PREFIX="$PREFIX" > /dev/null cmake --install "$BASE_DIR/third_party/nlohmann-build" > /dev/null fi if [ ! -x "$BUILD/bin/laya-cli" ]; then # LAYA_CUDA defaults to ON and would require nvcc, so it must be turned off # explicitly. BUILD_TESTING=OFF skips test binaries that want GPU fixtures. echo "==> Building laya-cli (CPU only)" cmake -S "$LAYACPP" -B "$BUILD" -G Ninja \ -DCMAKE_BUILD_TYPE=Release \ -DLAYA_CUDA=OFF -DLAYA_VULKAN=OFF -DLAYA_COREML=OFF \ -DBUILD_TESTING=OFF \ -DCMAKE_PREFIX_PATH="$PREFIX" > /dev/null cmake --build "$BUILD" --parallel "$(nproc)" fi echo echo "Built: $BUILD/bin/laya-cli" "$BUILD/bin/laya-cli" --help > /dev/null && echo "Smoke check OK." echo echo "Try it:" echo " ./usage-classify.sh \"Saya dikenakan biaya dua kali, tolong refund.\"" echo echo "For repeat calls, keep the checkpoint resident (about 8x faster):" echo " setsid $BUILD/bin/laya-cli --server --port 8080 \\" echo " --model $BASE_DIR/models/convaiinnovations/laya --variant multilingual --cpu &" echo " LAYA_URL=http://127.0.0.1:8080 ./usage-classify.sh \"...\""