Skip to content

memagent: in-process llama.cpp engine, selectable against the Python worker - #2

Merged
FMXExpress merged 3 commits into
mainfrom
claude/optimistic-allen-i3bd3d
Oct 29, 2026
Merged

FMXExpress merged 3 commits into
mainfrom
claude/optimistic-allen-i3bd3d

Conversation

@FMXExpress

Copy link
Copy Markdown
Owner

What this adds

The model can now run inside the Pascal program through llama.cpp, with no Python while chatting. It is a module behind the existing TEngine interface, so you can pick either engine:

[agent]
engine=python   ; Transformers + PEFT worker (default, unchanged)
engine=llama    ; llama.cpp in-process: GGUF base model + GGUF LoRA

You can also pass --engine llama for a single run. memagent.lpr is the only place that knows which engine is running; the agent loop, store, retrieval and guard did not change.

New units

  • Mem.Llama: binds the llama.cpp calls the engine needs, at tag b11985. The library is loaded at run time, so memagent still builds and runs on the Python engine without it. Record sizes are checked against the C compiler at compile time, so a mismatched header fails the build instead of corrupting memory. It also masks FPU exceptions, which ggml needs.
  • Mem.Engine.Llama (TLlamaEngine):
    • Prompt and decoding: the model's own chat template. Decoding is greedy at temperature 0, or top-p sampling otherwise, with an HF-style repetition penalty of 1.1 over prompt and reply.
    • Streaming: tokens are streamed without splitting UTF-8 characters.
    • LoRA: hot-swapped with llama_adapter_lora_init and llama_set_adapters_lora. /ab detaches the adapter for a single call.
    • GPU: the model is pinned to [gpu] infer, so the training GPU stays free.
  • Mem.Extract: a Pascal port of the Python fact extraction (rules, has_statement, the LLM extraction prompt, JSON parsing and grounding). A parity test gives the same result as Python on all 458 checks (memagent/tests/gen_extract_cases.py, extract_test.lpr).

GGUF pipeline (training stays in Python)

  • trainer/gguf_export.py: converts the base model, and each adapter, to GGUF.
  • Consolidation: consolidate.py --gguf writes adapter.gguf before ready.json, so the swap never waits for a conversion. It adds 3–4 s per consolidation.
  • Older adapters: an adapter trained under the Python engine is converted the first time the llama engine loads it, so a data folder can switch engines at any time.
  • Settings: models.json gets a "gguf" entry per profile. medium and large convert to q8_0; large converts from the full-precision Magistral weights, because its training base is bnb-4bit.
  • Setup: scripts/llama_setup.sh (make llama) checks out and builds llama.cpp b11985 as shared libraries in cache/llama, so the folder can be moved. It also converts the base models. LLAMA_CUDA=1 builds the CUDA backend.

Results (bench/README.md)

30 facts, tiny profile, 4-core CPU, temperature 0, both runs on the same build:

Engine Accuracy before / after sleep Confabulation Time per turn Total run
python 0.317 / 0.80 0.20 1548 ms 523 s
llama 0.35 / 0.80 0.20 1293 ms 386 s
  • Quality: the same LoRA gives the same after-sleep scores on both engines. Before sleep, 3 of 80 answers differ (f16 vs fp32 arithmetic). On the prompts checked by hand, replies are word-for-word identical.
  • First token: Python is still faster here, 823 ms against 973 ms.
  • CPU threads: using all 4 cores was 2.4× slower than 3, because ggml's threads compete with the agent thread and ONNX Runtime. The default is now all cores but one.

Testing

  • make test: extraction parity (458/458), retrieval and TLS checks, and the Python unit tests (22, including the new GGUF settings tests).
  • make test-e2e-llama: the full e2e suite on the llama engine passes (two consolidations, a correction, /forget, /rollback, non-ASCII facts).
  • bench/bench_agent.py --engine python|llama: the comparison above.

Not tested

  • The CUDA build of llama.cpp.
  • small, medium and large on the llama engine.
  • macOS and Windows library loading (the library names are there, but neither platform has been run).

Generated by Claude Code

- Mem.Llama: dynlibs binding to llama.h at tag b11985, record layouts
  asserted at compile time
- Mem.Engine.Llama: TLlamaEngine (chat template, HF-style repetition
  penalty, UTF-8-safe streaming, LoRA hot swap via llama_set_adapters_lora)
- Mem.Extract: Pascal port of memlib fact extraction, parity test against
  cases dumped from Python (458/458)
- [agent] engine=python|llama, --engine, [llama] section
- trainer/gguf_export.py; consolidate.py --gguf writes adapter.gguf before
  ready.json; engine converts older adapters on first load
- scripts/llama_setup.sh, make llama, make test-e2e-llama

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NPVFJHeBeotAKkU7EPbTgn
Four ggml threads on four cores ran 2.4x slower than three (spinning
workers contend with the agent thread and ONNX Runtime). Python vs llama
on the 30-fact system benchmark: same accuracy, 1.29 s vs 1.55 s per turn.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01NPVFJHeBeotAKkU7EPbTgn
@FMXExpress
FMXExpress merged commit aef4625 into main Oct 29, 2026
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