Fast local LLM inference on plain CPUs. No GPU, 8 GB of RAM. OpenAI-compatible server.
Python aiavx512cpu-inferenceedge-aiggufinference-enginellama-cppllmllm-inferencelocal-ailocal-llmno-gpuon-device-aiopenai-apiopenai-compatiblequantizationqwenself-hostedspeculative-decodingternary
fix(install): stage native binaries before replacing the working engine (#109)
Afloatcdcf6109e16e
diff --git a/install.sh b/install.shindex 0dac664..9b6fc35 100755--- a/install.sh+++ b/install.sh@@ -128,10 +128,7 @@ if [ "$BUILD" = 1 ]; then for t in cmake c++; do command -v $t >/dev/null || die# ---------------------------------------------------------------- engineSRC=$PREFIX/llama.cpp-if [ "$MODEL_ONLY" = 1 ]; then- :-elif [ -n "$BIN_URL" ]; then- say "Fetching prebuilt binaries: $BIN_URL"+begin_bin_update() {BIN_STAGE=$(mktemp -d "$PREFIX/.bin.XXXXXX")BIN_PREVIOUS=$BIN_STAGE.previouscleanup_bin_update() {@@ -153,6 +150,26 @@ elif [ -n "$BIN_URL" ]; thentrap cleanup_bin_update EXITtrap 'exit 130' INTtrap 'exit 143' TERM+}++publish_bin_update() {+ mv "$PREFIX/bin" "$BIN_PREVIOUS"+ if mv "$BIN_STAGE" "$PREFIX/bin"; then+ :+ else+ update_status=$?+ echo "could not publish the engine; restoring the previous binaries" >&2+ exit "$update_status"+ fi+ rm -rf "$BIN_PREVIOUS"+ trap - EXIT INT TERM+}++if [ "$MODEL_ONLY" = 1 ]; then+ :+elif [ -n "$BIN_URL" ]; then+ say "Fetching prebuilt binaries: $BIN_URL"+ begin_bin_updatecurl -fSL --retry 3 "$BIN_URL" | tar -xz -C "$BIN_STAGE" --strip-components=1 \|| die "could not download or unpack $BIN_URL"# a prebuilt engine only runs on the distribution it was built on or a newer one: find out now, before the download@@ -166,16 +183,7 @@ elif [ -n "$BIN_URL" ]; thenA missing library or a 'version ... not found' line means it was built on a newer distribution than this one.Run plain ./install.sh to build the engine here instead."fi- mv "$PREFIX/bin" "$BIN_PREVIOUS"- if mv "$BIN_STAGE" "$PREFIX/bin"; then- :- else- update_status=$?- echo "could not publish the prebuilt engine; restoring the previous binaries" >&2- exit "$update_status"- fi- rm -rf "$BIN_PREVIOUS"- trap - EXIT INT TERM+ publish_bin_updateelsestamp="$FORK_SHA portable=$PORTABLE $(cat "$HERE"/patches/*.patch "$HERE"/kernels/* | cksum | cut -d' ' -f1)"fi@@ -221,10 +229,17 @@ elif [ "$BUILD" = 1 ]; then[ "$jobs" -lt 1 ] && jobs=1cmake --build "$SRC/build" -j "$jobs" >>"$PREFIX/build.log" 2>&1 \|| { tail -30 "$PREFIX/build.log"; die "build failed (log: $PREFIX/build.log)"; }- rm -f "$PREFIX"/bin/*- cp -a "$SRC"/build/bin/llama-server "$SRC"/build/bin/llama-bench "$SRC"/build/bin/llama-cli "$PREFIX/bin/" 2>/dev/null || true- cp -a "$SRC"/build/bin/*.so* "$PREFIX/bin/" 2>/dev/null || true- echo "$stamp" > "$PREFIX/bin/.stamp"+ begin_bin_update+ cp -a "$SRC/build/bin/llama-server" "$BIN_STAGE/"+ for artifact in "$SRC/build/bin/llama-bench" "$SRC/build/bin/llama-cli" "$SRC"/build/bin/*.so*; do+ [ -e "$artifact" ] || continue+ cp -a "$artifact" "$BIN_STAGE/"+ done+ echo "$stamp" > "$BIN_STAGE/.stamp"+ if ! LD_LIBRARY_PATH="$BIN_STAGE" "$BIN_STAGE/llama-server" --version >>"$PREFIX/build.log" 2>&1; then+ die "the built engine cannot start (log: $PREFIX/build.log); keeping the previous engine"+ fi+ publish_bin_updatefi[ -x "$PREFIX/bin/llama-server" ] || die "no llama-server in $PREFIX/bin"diff --git a/tests/test_install_native_transaction.py b/tests/test_install_native_transaction.pynew file mode 100644index 0000000..72819c0--- /dev/null+++ b/tests/test_install_native_transaction.py@@ -0,0 +1,92 @@+import os+from pathlib import Path+import shutil+import subprocess+import tempfile+import unittest++ROOT = Path(__file__).resolve().parents[1]+++class NativeInstallTransactionTest(unittest.TestCase):+ def setUp(self):+ self.directory = tempfile.TemporaryDirectory()+ self.addCleanup(self.directory.cleanup)+ self.root = Path(self.directory.name)+ self.prefix = self.root / 'installation'+ self.tools = self.root / 'tools'+ self.tools.mkdir()+ (self.prefix / 'bin').mkdir(parents=True)+ (self.prefix / 'models').mkdir()+ (self.prefix / 'llama.cpp/.git').mkdir(parents=True)+ (self.prefix / 'models/model.gguf').write_bytes(b'cached model')+ self.previous = b'#!/bin/sh\nexit 0\n# working engine\n'+ self.command(self.prefix / 'bin/llama-server', self.previous.decode())+ (self.prefix / 'bin/.stamp').write_text('previous build')+ for name in ('dirname', 'uname', 'grep', 'cut', 'sed', 'awk', 'cat', 'mkdir', 'id',+ 'mv', 'rm', 'mktemp', 'head', 'nproc', 'cksum', 'chmod', 'ninja'):+ executable = shutil.which(name)+ if executable:+ (self.tools / name).symlink_to(executable)+ self.command(self.tools / 'df', "#!/bin/sh\nprintf 'Filesystem 1024-blocks Used Available Capacity Mounted\\nfixture 999999999 0 999999999 0%% /\\n'\n")+ self.command(self.tools / 'du', "#!/bin/sh\nprintf '0 fixture\\n'\n")+ for name in ('curl', 'python3', 'git', 'c++', 'sh'):+ self.command(self.tools / name, '#!/bin/sh\nexit 0\n')+ self.command(self.tools / 'cmake', '''#!/bin/sh+if [ "$1" = --build ]; then+ mkdir -p "$2/bin"+ printf '#!/bin/sh\\nexit %s\\n# replacement engine\\n' "$ENGINE_EXIT" > "$2/bin/llama-server"+ printf '#!/bin/sh\\nexit 0\\n' > "$2/bin/llama-bench"+ printf '#!/bin/sh\\nexit 0\\n' > "$2/bin/llama-cli"+ chmod +x "$2/bin/llama-server" "$2/bin/llama-bench" "$2/bin/llama-cli"+fi+''')+ self.command(self.tools / 'cp', '''#!/bin/sh+for arg in "$@"; do destination=$arg; done+if [ "$COPY_FAIL" = 1 ]; then+ printf truncated > "$destination/llama-server"+ exit 37+fi+exec "''' + shutil.which('cp') + '''" "$@"+''')+ self.env = dict(os.environ, OPENSET_LLM_HOME=str(self.prefix), PATH=str(self.tools),+ ENGINE_EXIT='0', COPY_FAIL='0')+ self.env.pop('OPENSET_LLM_BIN_URL', None)++ def command(self, path, source):+ path.write_text(source)+ path.chmod(0o755)++ def install(self):+ return subprocess.run(['/bin/bash', str(ROOT / 'install.sh'), '--no-mtp'], env=self.env,+ capture_output=True, text=True, timeout=15)++ def assert_previous_engine(self):+ self.assertEqual((self.prefix / 'bin/llama-server').read_bytes(), self.previous)+ self.assertEqual((self.prefix / 'bin/.stamp').read_text(), 'previous build')+ self.assertEqual(list(self.prefix.glob('.bin.*')), [])++ def test_partial_copy_failure_preserves_working_engine(self):+ self.env['COPY_FAIL'] = '1'+ result = self.install()+ self.assertNotEqual(result.returncode, 0)+ self.assert_previous_engine()++ def test_unusable_built_server_preserves_working_engine(self):+ self.env['ENGINE_EXIT'] = '11'+ result = self.install()+ self.assertNotEqual(result.returncode, 0)+ self.assert_previous_engine()++ def test_success_replaces_engine_and_stamp_together(self):+ (self.prefix / 'bin/obsolete.so').write_bytes(b'old backend')+ result = self.install()+ self.assertEqual(result.returncode, 0, result.stderr)+ self.assertIn(b'replacement engine', (self.prefix / 'bin/llama-server').read_bytes())+ self.assertNotEqual((self.prefix / 'bin/.stamp').read_text(), 'previous build')+ self.assertFalse((self.prefix / 'bin/obsolete.so').exists())+ self.assertEqual(list(self.prefix.glob('.bin.*')), [])+++if __name__ == '__main__':+ unittest.main()