Skip to content

Patches: decode OOM batch recovery (0014) and low-memory grammar sampling (0019) - #100

Open
Banan412 wants to merge 1 commit into
cobanov:mainfrom
Banan412:split/2-oom-safety
Open

Banan412 wants to merge 1 commit into
cobanov:mainfrom
Banan412:split/2-oom-safety

Conversation

@Banan412

@Banan412 Banan412 commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Decode and grammar-sampling memory safety fixes split out of #89, with live PS5 before/after reproduction.

Patch Numbering

Changes

  • 0014-decode-bad-alloc-recovery.patch (tools/server/server-context.cpp):
    • Catches std::bad_alloc during llama_process/llama_synchronize (yield_to_queue) and update_slots batch processing, clears idle slots and introspect_capture, and halves n_batch down to 1 to retry.
    • Documents inline at both retry sites that std::bad_alloc inside llama_decode skips its KV rollback (seq_rm), so a retry can still fail safely with "Invalid input batch".
    • When n_batch == 1 or a slot callback still throws std::bad_alloc, sends a server error, runs slot.prompt_clear(), clears introspect_capture, and releases the slot cleanly instead of crashing.
  • 0019-grammar-low-memory.patch (src/llama-grammar.cpp):
    • Packs all candidate token UTF-8 codepoints into a single contiguous decoded_cp_buf (decode_utf8_into) inside llama_grammar_apply_impl instead of allocating up to cur_p->size (248,320 on Qwen3.5) separate std::vector<uint32_t> heap buffers, and drops decoded_meta before rejection.
    • Caps speculative rejects / next_candidates reserves in llama_grammar_reject_candidates_for_stack at min(candidates.size(), 64) and returns early when next_candidates is empty.

Verification (Live PS5 Before / After Repro)

  • Workload: qwen3.5-18b-a3b-reap-coding-heretic-v0.i1-Q4_K_M.gguf (11.38 GB, vocab 248320, LLAMA_INTROSPECT=1, strict nested character_sheet json_schema response_format).
  • BEFORE (origin/main patches): Server immediately closed the socket (Remote end closed connection without response) and crashed/restarted PS5LM.
  • AFTER (split/2-oom-safety at 0.14 GiB free): Completed HTTP 200 (139 tokens — 19 prompt / 120 completion, 1.56 s, 83.82 tok/s), and a follow-up normal chat turn completed HTTP 200 (32 tokens, 0.31 s).
  • Re-verified across multiple models on updated stacked build (split/ps5-all, fa0fc59 base): Identical strict nested character_sheet json_schema completed HTTP 200, valid JSON, and /health 200 {"status":"ok"} on:
    • qwen3.5-18b-a3b (16k q8_0, LLAMA_INTROSPECT=1)
    • Qwen3.5-2B (64k q8_0, both Off and On, 8.51 GiB free)
    • Qwen3.5-9B (64k q8_0 On, 2.01 GiB free)
    • Qwen3.6-12B (64k q8_0 On, 2.46 GiB free)
    • qwen3.5-14b-a3b (64k q8_0 On, 2.05 GiB free)
    • Qwen3.8-27B IQ2_M (32k q8_0 Off at 0.11 GiB free, and 64k q4_0 On at 0.12 GiB free)
  • Patch & compile checks: scripts/apply-patches.sh passed with exit 0 and zero 100755/mode warnings on both origin/main (970b51ce7) and #103 (d2bceda3d + host llama-server build).

Refs #89.

Console Screenshot

Live PS5 TV HUD showing qwen3.5-18b-a3b completing plain text and strict character_sheet JSON replies
Live PS5 TV HUD (qwen3.5-18b-a3b, non-thinking capture) showing plain text and the nested character_sheet JSON output after passing.


Generated with Google's Antigravity

@cobanov cobanov left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the before/after repro; that's exactly what I hoped for, and both patches are welcome.

Two things before it goes in:

  • #103 (llama.cpp to b12600) takes patches/0019, and neither patch applies on b12600. Please rebase once #103 lands and move the grammar patch to the next free number (0014 can stay).
  • When you regenerate them, please use file mode 100644; git apply currently warns about 100755.

One optional note: a bad_alloc inside llama_decode skips its KV rollback (seq_rm), so a retry may end in "Invalid input batch". It fails safely, but a comment in the patch saying so would help.

@Banan412

Copy link
Copy Markdown
Contributor Author

@cobanov Updated split/2-oom-safety (6209281) and PR #100 with your feedback:

@cobanov

cobanov commented Nov 25, 2026

Copy link
Copy Markdown
Owner

@Banan412 #103 is in (ead7971, llama.cpp d2bceda3d). Please push the versions you pre-ported. 0019 can keep its number, since #103 didn't take it. Once CI is green on the new pin, this goes in.

@Banan412

Copy link
Copy Markdown
Contributor Author

@cobanov Pushed the d2bceda3d pre-port rebased onto main (9766ad9, including #103 ead7971 and #112 0022) to split/2-oom-safety (0882305), keeping 0014 and 0019 (100644 headers, inline seq_rm comment preserved).

  • Verified scripts/apply-patches.sh applies all patches cleanly with zero warnings on d2bceda3d.
  • Verified host checks (bash -n, py_compile, test_build_app.py 7/7, model_plan_test, log_sink_test) and a full PS5 cross-build (scripts/build-app.sh, [522/522], eboot.bin 109,451,030 B) on the new pin.

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.

2 participants