Repository navigation
In-process LoRA training on llama.cpp; llama is the default trainer - #5
Merged
Merged
Conversation
Stock llama.cpp b11985 trains only the base model's own weights. The patch: - llama_adapter_lora_init_new: a fresh LoRA (A kaiming-uniform, B zero, like PEFT) - llama_opt_init also marks the attached adapters' tensors trainable, so a param_filter that rejects every model tensor trains the adapter alone - label -1 = masked position (prompt, padding), and the CPU cross-entropy backward uses softmax*sum(labels) - labels, so a masked row has no gradient (with softmax - labels the masked rows flattened the model into noise) - llama_adapter_lora_save: write the trained adapter as a loadable LoRA GGUF - the training graph budget counts the LoRA backward nodes lora_train_spike.cpp trains two facts on the tiny model, saves, reloads and hot-swaps: the adapter answers a held-out paraphrase; the base is unchanged. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01NPVFJHeBeotAKkU7EPbTgn
Mem.Train ports trainer/consolidate.py to Pascal: the active facts and their paraphrases, anchors and held-out anchors, a fresh LoRA trained on llama.cpp's optimizer (AdamW, warmup/decay, grad clipping, loss on the answer tokens), the recall/drift gate, adapter.gguf + manifest + ready.json for the hot swap. It runs on a background thread with its own f32 copy of the base model. [consolidate] trainer=llama is the default; it falls back to the Python trainer (and says why) for the python engine, an unpatched llama.cpp and the medium/large profiles. The llama.cpp patch now carries the training API and fixes two training bugs, both verified against PyTorch + PEFT: - the cross-entropy backward gave masked (all-zero) label rows the softmax as gradient, flattening the model; now softmax * sum(labels) - labels (CPU, CUDA) - with graphs rebuilt per step and opt_period 1, ggml never reset the parameter gradient accumulators: every step used the sum of all gradients so far, and training diverged even at near-zero learning rate After the fixes, LoRA gradients match PEFT on all 420 tensors (cosine 1.0) and step-by-step losses to 1e-4. 30-fact benchmark: recall 1.00 and held-out drift 1.22x (python 1.00, 1.33x); make test-e2e-llama passes with the in-process trainer. Also: Mem.LlamaChat (generation shared by engine and trainer), thread-safe llama.cpp logging, per-thread FPU masking, release smoke tests now train on Linux and Windows, Windows release builds apply the patch. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_01NPVFJHeBeotAKkU7EPbTgn
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this adds
Long-term memory now trains inside memagent too. With the defaults (
engine=auto,trainer=llama), chat, retrieval, LoRA training and the hot swap all run in one Free Pascal process on llama.cpp, with no Python.Mem.Train: the trainer, in Pascal
A step-for-step port of
trainer/consolidate.py, running on a background thread with its own f32 copy of the base model:adapter.gguf,manifest.jsonandready.json. The agent and the hot swap are unchanged.Which trainer runs
[consolidate] trainer=llamais the default. It falls back to the Python trainer, and says why at startup, for:medium/large, marked"trainer": "python"inmodels.json, because their f32 copies would be 16 GB / 96 GB.The llama.cpp patch (
packaging/llama-patches/0001-lora-training.patch)Stock b11985 trains only a model's own weights. The patch adds:
llama_adapter_lora_init_new,llama_lora_train_init,llama_lora_train_epoch,llama_adapter_lora_saveandllama_model_convert_f32.softmax − labelsgives an all-zero (masked) row the full softmax as its gradient. It is nowsoftmax·sum(labels) − labels, on CPU and CUDA.opt_period = 1, ggml never reset the parameter gradient accumulators, so step n trained on the sum of all gradients so far. In-session step 1 = fresh step 1 + step 0, cosine 1.0000. Training looked fine for a few steps, then diverged even at near-zero learning rate. This also affects stock llama.cpp's finetune example.After the fixes:
Results (
bench/README.md)30 facts, tiny profile, 4-core CPU:
make test-e2e-llamapasses on the in-process trainer: two consolidations, a correction,/forgetand/rollback, with hot swaps of 14–40 ms.make testpasses.Release workflow
/sleep, and the run must showtraining: in-process on llama.cppand a hot-swapped LoRA. That is the first run of the trainer on Windows.Not tested
smallon the in-process trainer.Generated by Claude Code