#!/usr/bin/env bash # Install this self-contained TT model package into an isolated, reproducible venv (via uv). # Usage: ./install.sh [venv-path] (default: ./venv) # Deps: v6 thin: ttnn/tt-metal-models (index) + empty-target vLLM + plugin/ops wheels (by path) # # HERMETIC INSTALL: everything the model needs to SERVE ends up UNDER this folder — the pinned # interpreter (in .python/), the venv (with package contents copied in), and at serve time the # caches/weights (run.sh points HF_HOME/TT_CACHE_PATH/... here). After this runs, serving depends # on nothing outside the folder except the TT device, system libc, and the exact externally managed # SFPI version declared by TTNN. Only THIS install step reaches the network (to fetch the interpreter # and, unless --vendor-deps, the pip deps). set -euo pipefail HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" VENV="${1:-$HERE/venv}" PYVER="3.12" UVVER="" # uv provisions the selected interpreter and applies the staged dependency closure. When the # bundle declares TT_MODEL_UV_VERSION, reject a different host uv and bootstrap that exact release. if ! command -v uv >/dev/null 2>&1 || { [ -n "$UVVER" ] && [ "$(uv --version 2>/dev/null | awk '{print $2}')" != "$UVVER" ]; }; then export UV_INSTALL_DIR="$HERE/.uv" curl -LsSf https://astral.sh/uv/install.sh | sh >/dev/null 2>&1 export PATH="$HERE/.uv:$PATH" fi # Keep the interpreter, its executable links, and uv's potentially large build/download cache # INSIDE the bundle rather than silently filling the host's ~/.local and ~/.cache. The cache is # installation-only and is removed after a successful install. export UV_PYTHON_INSTALL_DIR="$HERE/.python" export UV_PYTHON_BIN_DIR="$HERE/.python/bin" export UV_CACHE_DIR="$HERE/.uv-cache" uv python install "$PYVER" uv venv --relocatable --python "$PYVER" "$VENV" PYBIN="$VENV/bin/python" TT_MODEL_CONSTRAINTS_ARGS=() if [ -f "$HERE/constraints.txt" ]; then TT_MODEL_CONSTRAINTS_ARGS=(--constraint "$HERE/constraints.txt") fi uv pip install --python "$VENV/bin/python" --link-mode=copy ${TT_MODEL_CONSTRAINTS_ARGS[@]+"${TT_MODEL_CONSTRAINTS_ARGS[@]}"} --extra-index-url https://download.pytorch.org/whl/cpu -r "$HERE/requirements.txt" TTNN_DIR="$("$PYBIN" -I -c 'import importlib.util,pathlib;s=importlib.util.find_spec("ttnn");assert s and s.origin;print(pathlib.Path(s.origin).resolve().parent)')" sfpi_meta="$TTNN_DIR/tt_metal/sfpi-version" sfpi_version="" sfpi_build="" if [ -f "$sfpi_meta" ]; then sfpi_requirements="$("$PYBIN" -I - "$sfpi_meta" <<'PY' import re import sys from pathlib import Path values = {} for line in Path(sys.argv[1]).read_text(encoding="utf-8").splitlines(): match = re.fullmatch(r"(sfpi_(?:version|build))='([0-9.]+)'", line) if match: values[match.group(1)] = match.group(2) if set(values) != {"sfpi_version", "sfpi_build"}: raise SystemExit("invalid or incomplete TTNN sfpi-version metadata") print(values["sfpi_version"], values["sfpi_build"], sep="\t") PY )" IFS=$'\t' read -r sfpi_version sfpi_build <<<"$sfpi_requirements" sfpi_want="${sfpi_version}[${sfpi_build}]" else # An older TTNN wheel declares no SFPI version: it is unknown, so skip that check. printf 'note: %s not found; skipping the SFPI version check\n' "$sfpi_meta" >&2 sfpi_want="an unknown version" fi local_sfpi="$TTNN_DIR/runtime/sfpi" system_sfpi=/opt/tenstorrent/sfpi if [ -e "$local_sfpi/compiler/bin/riscv-tt-elf-g++" ]; then selected_sfpi="$local_sfpi" elif [ -e "$system_sfpi/compiler/bin/riscv-tt-elf-g++" ]; then selected_sfpi="$system_sfpi" else printf 'SFPI missing: TTNN requires %s; install the matching external host package\n' \ "$sfpi_want" >&2 exit 1 fi gxx="$selected_sfpi/compiler/bin/riscv-tt-elf-g++" test -x "$gxx" || { printf 'SFPI compiler is not executable: %s\n' "$gxx" >&2; exit 1; } test -r "$selected_sfpi/include/sfpi_lib.h" || { printf 'SFPI headers are incomplete at %s\n' "$selected_sfpi" >&2 exit 1 } actual="$($gxx --version | sed -n '1p')" [ -z "$sfpi_version" ] || case "$actual" in *"tenstorrent/sfpi:${sfpi_version}[${sfpi_build}]"*) printf 'validated SFPI %s[%s] at %s\n' "$sfpi_version" "$sfpi_build" "$selected_sfpi" >&2 ;; *) # An OLDER SFPI lacks APIs the TTNN kernels use and fails at JIT compile, so it is fatal. found="$(printf '%s' "$actual" | sed -n 's/.*tenstorrent\/sfpi:\([0-9.]*\).*/\1/p')" if [ -n "$found" ] && [ "$(printf '%s\n%s\n' "$found" "$sfpi_version" | sort -V | head -1)" = "$found" ] \ && [ "$found" != "$sfpi_version" ] && [ "${TT_MODEL_PRINT:-0}" != "1" ]; then printf 'SFPI at %s is older than TTNN needs (want %s[%s], found: %s); its kernels will not compile. Install SFPI %s or newer.\n' \ "$selected_sfpi" "$sfpi_version" "$sfpi_build" "$actual" "$sfpi_version" >&2 exit 1 fi # A newer SFPI is usually compatible, so it only warns unless TT_MODEL_STRICT_SFPI=1. if [ "${TT_MODEL_STRICT_SFPI:-0}" = "1" ] && [ "${TT_MODEL_PRINT:-0}" != "1" ]; then printf 'SFPI mismatch at %s: need %s[%s], got: %s (TT_MODEL_STRICT_SFPI=1)\n' \ "$selected_sfpi" "$sfpi_version" "$sfpi_build" "$actual" >&2 exit 1 fi printf 'warning: SFPI at %s is not the version TTNN declares (want %s[%s], found: %s); continuing. Set TT_MODEL_STRICT_SFPI=1 to make this fatal.\n' \ "$selected_sfpi" "$sfpi_version" "$sfpi_build" "$actual" >&2 ;; esac uv pip check --python "$PYBIN" PYTHONPATH="$HERE/extra_code${PYTHONPATH:+:$PYTHONPATH}" "$VENV/bin/python" -c 'import importlib.metadata as m; assert m.version('"'"'ttnn'"'"') == '"'"'0.80.0'"'"', m.version('"'"'ttnn'"'"')' PYTHONPATH="$HERE/extra_code${PYTHONPATH:+:$PYTHONPATH}" "$VENV/bin/python" -c 'import models.autoports.minimaxai_minimax_music3.server.app as a; assert a.app' PYTHONPATH="$HERE/extra_code${PYTHONPATH:+:$PYTHONPATH}" "$VENV/bin/python" -c 'import models.autoports.minimaxai_minimax_music3.server.schemas as s; s.SpeechRequest(input='"'"'[verse]\nla'"'"', instructions='"'"'pop'"'"')' PYTHONPATH="$HERE/extra_code${PYTHONPATH:+:$PYTHONPATH}" "$VENV/bin/python" -c 'import music_client, music_page' PYTHONPATH="$HERE/extra_code${PYTHONPATH:+:$PYTHONPATH}" "$VENV/bin/python" -c 'import soundfile as sf; assert '"'"'WAV'"'"' in sf.available_formats() and '"'"'FLAC'"'"' in sf.available_formats()' PYTHONPATH="$HERE/extra_code${PYTHONPATH:+:$PYTHONPATH}" "$VENV/bin/python" -c 'import soxr, numpy; assert numpy.__version__.startswith('"'"'1.'"'"'), numpy.__version__' # The uv cache is install-only; drop it so it does not sit in the bundle beside the venv. rm -rf "$UV_CACHE_DIR" echo "installed into $VENV (python $PYVER, interpreter under $HERE/.python)"