Skip to content

Fast local LLM inference on plain CPUs. No GPU, 8 GB of RAM. OpenAI-compatible server.

fix(serve): normalize numeric options before context arithmetic (#64)

Afloat6111b1682f6b
diff --git a/serve b/serve
index fec3cb5..999afe8 100755
--- a/serve
+++ b/serve
@@ -57,6 +57,13 @@ case $MULTI in ''|*[!0-9]*) echo "--multi takes a number of streams (2-8)" >&2;
for v in "$PORT" "$THREADS" ${CTX:+"$CTX"} ${DEPTH:+"$DEPTH"}; do
case $v in ''|*[!0-9]*) echo "--port, --ctx, --threads and --spec-depth take a number (got: $v)" >&2; exit 2 ;; esac
done
+# Bash arithmetic treats leading zeroes as octal; CLI numbers are decimal.
+for numeric_name in PORT THREADS CTX MULTI DEPTH; do
+ numeric_value=${!numeric_name}
+ [ -n "$numeric_value" ] || continue
+ numeric_value=${numeric_value#"${numeric_value%%[!0]*}"}
+ printf -v "$numeric_name" '%s' "${numeric_value:-0}"
+done
CKPT=1; RESUME_MODE=full
if [ "$MULTI" -ge 2 ]; then
# X2: N streams share each pass over the weights. --ctx is per stream here. Speculation is off (X1/MTP + X2 is untested).
diff --git a/tests/test_serve_decimal.py b/tests/test_serve_decimal.py
new file mode 100644
index 0000000..fa48338
--- /dev/null
+++ b/tests/test_serve_decimal.py
@@ -0,0 +1,33 @@
+import json
+import os
+from pathlib import Path
+import subprocess
+import tempfile
+import unittest
+
+ROOT = Path(__file__).resolve().parents[1]
+
+
+class DecimalServeOptions(unittest.TestCase):
+ def test_zero_padded_context_and_streams_use_decimal_arithmetic(self):
+ with tempfile.TemporaryDirectory() as tmp:
+ prefix = Path(tmp)
+ (prefix / 'bin').mkdir()
+ (prefix / 'models').mkdir()
+ (prefix / 'models/model.gguf').touch()
+ server = prefix / 'bin/llama-server'
+ server.write_text('#!/usr/bin/env python3\nimport json,sys\nprint(json.dumps(sys.argv[1:]))\n')
+ server.chmod(0o755)
+ result = subprocess.run(['bash', str(ROOT / 'serve'), '--no-spec', '--no-resume',
+ '--ctx', '0008', '--multi', '02', '--threads', '0002', '--port', '08080'],
+ env={**os.environ, 'OPENSET_LLM_HOME': tmp}, text=True, capture_output=True, timeout=10)
+ self.assertEqual(result.returncode, 0, result.stderr)
+ args = json.loads(result.stdout)
+ self.assertEqual(args[args.index('-c') + 1], '16')
+ self.assertEqual(args[args.index('-np') + 1], '2')
+ self.assertEqual(args[args.index('-t') + 1], '2')
+ self.assertEqual(args[args.index('--port') + 1], '8080')
+
+
+if __name__ == '__main__':
+ unittest.main()