Skip to content

[Engine] multi-slot Host-RAM prefix pinning and strata_pin endpoint - #343

Open
LeoXing1996 wants to merge 2 commits into
mainfrom
leo/whi-1016-prefix-pin
Open

LeoXing1996 wants to merge 2 commits into
mainfrom
leo/whi-1016-prefix-pin

Conversation

@LeoXing1996

Copy link
Copy Markdown

Problem and result

Existing warm_start / single Slot pinned_ cannot keep multiple static Agent prefixes (such as Linger's meta and alter_ego) hot simultaneously on Inkling 2:

  1. keep_start() in terminal/serve/server.py stopped at the second <|message_system|> boundary, which on Inkling 2 (tool_declare -> <SYSTEM PROMPT> -> Thinking effort level) kept only tool_declare, dropped the system prompt, and skipped saving below 2048 tokens.
  2. Warm-up previously wrote .warm-start to disk without pinning in-process (PIN_CUR) and gave up for the rest of the process lifetime if preempted by an interactive request.
  3. Conversations::pinned_ held a single global slot, and Conversations::release() called Spill::release() on pinned slots, evicting heap KV allocations to /var/tmp and dropping pages.

This PR adds opt-in multi-slot Host-RAM prefix pinning (strata_pin) and fixes Inkling 2 multi-<|message_system|> truncation so meta + alter_ego prefixes stay hot in Host RAM orthogonally to parallel=1, reload automatically across server restarts via --pin-dir, and cut Inkling 2 Mini Metal TTFT from 2096.2 ms (COLD_META) / 1338.1 ms (COLD_ALTER) down to 246.2 ms (8.5x) / 253.6 ms (5.3x) on MAC-119. Requests without strata_pin and default CLI flags keep identical default behavior.

Related discussion

Approach

  • spill.hpp / spill.cpp / conversations.hpp:
    • Added pin_ram(), unpin_ram(), is_pinned_ram(), and has_heap(), guarding Spill::release() / Spill::write() so resident: "ram" pinned blobs stay hot in Host RAM.
    • Replaced single Slot pinned_ with named std::vector<PinnedEntry> pinned_ (pin_named, pin_current, unpin_named, unpin_all, pinned_count, pinned_info, CacheOptions::pin_mib LRU budget), orthogonal to active conversation slots (parallel=1). Conversations::resume() picks the longest matching prefix across all active/pinned slots and shares KV rows incrementally.
  • main.cpp:
    • Added prefill=1 (max_new=0, want_logits=false) to parse_gen / Session::generate.
    • Added --pin-dir, --pin-cache-mib, atomic <pin_dir>/<name>.bin persistence (PIN_CUR, PIN_LOAD, UNPIN, PINS), and automatic startup reload (pinned_slots= on INFO).
  • terminal/serve/server.py / server_parts/openai_api.py:
    • Fixed _system_turn_end in keep_start() and resolve_pin_prefix() for multi-<|message_system|> templates so tool_declare + system prompt are pinned while stopping cleanly before Thinking effort level: and <|message_user|>.
    • Added opt-in /v1/chat/completions strata_pin (name, prefill_only, resident, optional anchor), prioritizing strata_pin.prefill_only: true even when upstream gateways (such as Bifrost) clamp max_tokens to "max_completion_tokens": 16, returning 0 completion tokens.
    • Added preemptible warmup (real_requests_pending, pin_preempt, pin_idle_cond) with automatic idle retry.

Validation

Passed:

  • C++ unit tests (conversations_test.cpp + spill.cpp including multi_slot_host_ram_pins and resident_pin_skips_spill_and_respects_budget) on commit c2f313fbf tree (local macOS c++ -std=c++20 & MAC-119 c++ -std=c++20 -O2): conversations_test: 62168 checks passed.
  • Python targeted suites on MAC-119 (/Users/aaron/ps-metal/.venv/bin/python -m pytest) after the final edit:
    • terminal/serve/test_inkling.py: 51 passed, 3 skipped, 157 subtests passed in 0.42s (the 3 skips require $STRATA_INKLING_TOKENIZER).
    • terminal/serve/test_server.py -k StrataPinHttpAndPreemption: 3 passed, 467 deselected in 2.24s.
  • Live Metal build (-DSTRATA_METAL_POLY=ON) and benchmark on MAC-119 (Apple M5 Ultra 256 GiB, macOS 27.2.1, Inkling-2-Mini-UD-Q3_K_XL.gguf, parallel=1, --conversation-cache-mib 0, --max-context 16384):
    • Cold baselines: COLD_META (5,783 tok) ttft_ms: 2096.2; COLD_ALTER (3,709 tok) ttft_ms: 1338.1 (cached_tokens: 0).
    • Registered meta (5,447 tok, omitted anchor, max_tokens: 0, prefill_only: true, meta.bin 693,327,228 B) + alter_ego (3,373 tok, explicit anchor, max_tokens: 1, prefill_only: true, alter_ego.bin 625,358,100 B), plus Bifrost shape (max_completion_tokens: 16, prefill_only: true -> completion_tokens: 0, status: "pinned").
    • Alternating normal hits (no strata_pin param): PIN_HIT_ALTER_1 ttft_ms: 253.6 (5.3x, 3373/3714 cached), PIN_HIT_META_1 ttft_ms: 246.2 (8.5x, 5447/5788 cached); switched reasoning_effort: "low" -> 314.0 ms (3373 cached) / 350.0 ms (5447 cached).
    • Server restart auto-reload (0 warmup calls): alter_ego.bin (133 ms) & meta.bin (157 ms) reloaded at startup; immediate requests 516.4 ms (5447/5784 cached) and 249.6 ms (3373/3710 cached).
    • Preemption & idle retry: 5,777-tok interactive request preempted 4,171-tok warmup (ttft_ms: 456.6), warmup retried cleanly when idle (preempted: 1, status: "pinned").

Failed:

  • Prior to the final _system_turn_end (p + 64) and test_server.py mock fix, python tools/check --only py-tools on MAC-119 reported 2203 passed, 1 failed, 47 skipped (pre-existing M5 Ultra platform-default failure in terminal.tools.test_setup_hy4.MacHy4Flash::test_mac_install_plan_defaults_to_128k_and_unified_fit).
  • Standalone LoadSharedSettings::test_several_bad_keys_are_each_named in test_server.py fails on untouched http_api.py (top_k: 900 accepted; 1 failed, 6 passed, 463 deselected).

Not run:

  • Full python tools/check --only py-tools and full terminal/serve/test_server.py were not re-run after the final _system_turn_end + mock fix.
  • CUDA, HIP, SYCL, and Windows engine builds (main.cpp, conversations.hpp, spill.cpp) were not built or tested on hardware here (only macOS Metal + C++ unit test target).

Limits

  • Pin slots stay pinned in Host RAM up to --pin-cache-mib (LRU eviction above budget); --pin-dir persistence requires explicit opt-in (--pin-dir / --cache-dir).
  • Real multi-slot reuse with Linger Daemon automatic post-load warm-up is tracked separately in WHI-1019.

@LeoXing1996

Copy link
Copy Markdown
Author

Fixed the pre-existing main regression in LoadSharedSettings.test_several_bad_keys_are_each_named ("top_k": 900 -> -1, matching d2ecae2df allowing top_k >= 0) so Linux / Python terminal/serve/ and Linux / runtime requirements only can pass.

Also note from the previous CI run: Config snapshot / windows-2022 hit an 1800s runner timeout while ubuntu-24.04 passed; this push re-triggers CI cleanly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant