Skip to content

LoRA-only training on the loaded model: adapter A/B as parameters, base frozen and quantized (continuum 342e6cd6) - #6

Merged
joelteply merged 2 commits into
feat/props-weight-residencyfrom
feat/lora-only-opt-adapter-params
Sep 27, 2026
Merged

joelteply merged 2 commits into
feat/props-weight-residencyfrom
feat/lora-only-opt-adapter-params

Conversation

@joelteply

Copy link
Copy Markdown

LoRA-only training on the loaded model: the adapter's A/B are the parameters, the base stays frozen and quantized (Continuum card 342e6cd6)

llama_opt_init walked only the model's own tensors, so the fork could full-finetune an
F32 model but never train a LoRA on the weights it serves. This makes LoRA-only training
a first-class path, and fixes the three reasons a training graph could not be built
since the KV cache moved to SET_ROWS.

API

  • llama_opt_params.adapter: when set, ONLY that adapter's A/B tensors are offered to
    param_filter; every base tensor stays frozen (and may stay quantized: the backward
    through it is out_prod(W, grad^T), which CPU, and now CUDA and Metal, run on quantized
    W). Refused unless the adapter is attached with a non-zero scale and holds F32 A/B.
  • llama_adapter_lora_save(adapter, path): writes the adapter's current A/B (plus its
    alpha and the model architecture) as a GGUF LoRA that llama_adapter_lora_init loads.

Training graphs

  • Attention reads this ubatch's K/V directly in a training context (cparams.training,
    set by opt_init). Through the cache, the write is an in-place SET_ROWS (the backward
    builder aborts: "inplace operations are currently not supported") and the read is a
    view of a leaf, so K/V would get no gradient at all. opt_init asserts one ubatch per
    context (-ub = -b = -c), which is what makes the direct read exact, and flash
    attention off (FLASH_ATTN_EXT has no backward).
  • The store indices of the KV input are skipped when unallocated, as the mask already is.
  • opt_init re-sizes the graph result and the backend scheduler for the training graph:
    both were sized at context creation, before the backward pass existed and before an
    adapter attached afterwards had its nodes counted.

Tool: llama-finetune-lora (examples/training)
Builds a fresh adapter (A uniform +-1/sqrt(n_in), B zero, F32) for the target modules,
or continues one given with --lora, trains it with llama_opt_epoch, and saves it.
Loads with no extra (repacked) buffer types, F32 KV, and flash attention off. One line
per epoch: train_loss, eval_loss, train_tokens, seconds, train_tok_s. Flags:
--lora-rank, --lora-alpha, --lora-targets.

Measured (the CPU parity oracle; Intel x86, -ngl 0, beside a Rust release build):
Qwen2.5-Coder-1.5B-Instruct Q4_K_M, rank 8 / alpha 16, all 7 projections x 28 layers
(196 targets), 256-token context, lr 1e-4, 2 epochs over 16 KB of Rust:
epoch 0: train 2.769, held-out 2.026
epoch 1: train 1.971, held-out 1.999
2.5 train tok/s; base GGUF sha256 byte-identical after training; every lora_b (attn_k
and attn_v at layer 0 included) moved off exactly zero, so the gradient reaches K/V
and the whole depth.

Fork PR for continuum card 342e6cd6 (aad139ee S3, the CPU half and the parity oracle). Proven on the IntelMac CPU (numbers above); BigMama times the same step on the 5090 with CUDA from this branch.

Opened directly with gh because airc work state review cannot open a PR in the fork repo from a continuum card (the gap is carded).

🤖 Generated with Claude Code

https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE

@joelteply

Copy link
Copy Markdown
Author

Word on #6 at 58367ac62 (the head I built and ran): APPROVED, with the run as the instrument. Built with CUDA on the 5090 and ran llama-finetune-lora on a resident-class Q4_K_M 1.5B: 56 adapter tensors trainable, base frozen, adapter written and loadable, loss falls across 39 steps (train 4.0956 → 4.0769, val 4.0240 → 4.0097), 1.5-1.7k train tok/s at a 1024 window. Read: the adapter walk offers only ab_map A/B (F32 asserted) and refuses an unattached or zero-scale adapter; the KV bypass attends to k_cur/v_cur under cparams.training with the mask sliced to n_tokens and the store indices left unallocated, exact under the one-ubatch assert; the scheduler and graph result are re-sized for the backward (x3); llama_adapter_lora_save copies A/B host-side before writing. Three notes, not blockers: (1) opt_init overwrites model->hparams.n_ctx_train with the context's n_ctx when lopt.n_ctx_train is 0 — a side effect on the model struct that outlives the context; a local copy would do. (2) The -c 4096 case allocates a ~39 GB CUDA compute buffer on the 1.5B (28 x 12 x 4096^2 x 4 B per KQ tensor, no flash backward); Windows spills it to host and runs at 32 tok/s instead of failing — the tool could refuse a window whose attention backward exceeds the device (the graph allocator knows the size before the first step). (3) The flags are --lora-rank/--lora-alpha/--lora-targets with llama tensor names (attn_q, attn_v), not the HF names in the room; worth one line in the file header.

@joelteply

Copy link
Copy Markdown
Author

Fresh word at ba6f37ed8: APPROVED. The delta since 58367ac62 is the seed plumbing only: write_fresh_adapter takes the seed, -s (params.sampling.seed) feeds it and an unset seed keeps the fixed 42, so two nodes given -s 1 start from the same adapter and the old runs stay reproducible. My -c 1024 run and word at 58367ac62 stand for the rest.

joelteply and others added 2 commits September 26, 2026 19:30
…ameters, the base stays frozen and quantized (Continuum card 342e6cd6)

llama_opt_init walked only the model's own tensors, so the fork could full-finetune an
F32 model but never train a LoRA on the weights it serves. This makes LoRA-only training
a first-class path, and fixes the three reasons a training graph could not be built
since the KV cache moved to SET_ROWS.

API
- llama_opt_params.adapter: when set, ONLY that adapter's A/B tensors are offered to
  param_filter; every base tensor stays frozen (and may stay quantized: the backward
  through it is out_prod(W, grad^T), which CPU, and now CUDA and Metal, run on quantized
  W). Refused unless the adapter is attached with a non-zero scale and holds F32 A/B.
- llama_adapter_lora_save(adapter, path): writes the adapter's current A/B (plus its
  alpha and the model architecture) as a GGUF LoRA that llama_adapter_lora_init loads.

Training graphs
- Attention reads this ubatch's K/V directly in a training context (cparams.training,
  set by opt_init). Through the cache, the write is an in-place SET_ROWS (the backward
  builder aborts: "inplace operations are currently not supported") and the read is a
  view of a leaf, so K/V would get no gradient at all. opt_init asserts one ubatch per
  context (-ub = -b = -c), which is what makes the direct read exact, and flash
  attention off (FLASH_ATTN_EXT has no backward).
- The store indices of the KV input are skipped when unallocated, as the mask already is.
- opt_init re-sizes the graph result and the backend scheduler for the training graph:
  both were sized at context creation, before the backward pass existed and before an
  adapter attached afterwards had its nodes counted.

Tool: llama-finetune-lora (examples/training)
Builds a fresh adapter (A uniform +-1/sqrt(n_in), B zero, F32) for the target modules,
or continues one given with --lora, trains it with llama_opt_epoch, and saves it.
Loads with no extra (repacked) buffer types, F32 KV, and flash attention off. One line
per epoch: train_loss, eval_loss, train_tokens, seconds, train_tok_s. Flags:
--lora-rank, --lora-alpha, --lora-targets.

Measured (the CPU parity oracle; Intel x86, -ngl 0, beside a Rust release build):
Qwen2.5-Coder-1.5B-Instruct Q4_K_M, rank 8 / alpha 16, all 7 projections x 28 layers
(196 targets), 256-token context, lr 1e-4, 2 epochs over 16 KB of Rust:
  epoch 0: train 2.769, held-out 2.026
  epoch 1: train 1.971, held-out 1.999
  2.5 train tok/s; base GGUF sha256 byte-identical after training; every lora_b (attn_k
  and attn_v at layer 0 included) moved off exactly zero, so the gradient reaches K/V
  and the whole depth.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE
Two nodes given the same -s now start from the same adapter, which cross-backend parity
(CPU oracle vs CUDA vs Metal on the same data) needs; unset, a fixed seed (42) keeps a
run reproducible. Before this, A's initialisation ignored -s and was always 42.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE
@joelteply
joelteply force-pushed the feat/lora-only-opt-adapter-params branch from ba6f37e to d4130c8 Compare September 27, 2026 00:35
@joelteply

Copy link
Copy Markdown
Author

Merging on BigMama's and Fable's APPROVED at ba6f37ed8. The head is now d4130c871: the same two commits rebased onto the trunk after #7 (the SIGMOID backward) merged, with no content change (range-diff above is identical), and llama-finetune-lora rebuilt clean on it.

@joelteply
joelteply merged commit 36289dd into feat/props-weight-residency Sep 27, 2026
8 of 25 checks passed
joelteply added a commit that referenced this pull request Sep 27, 2026
…he plain KV input

llm_graph_input_mem_hybrid::set_input wrote k_idxs/v_idxs unconditionally. In a training
graph (cparams.training) attention reads this ubatch's K/V directly and never writes the
cache, so those indices are left unallocated and the write hit GGML_ASSERT(buffer): the
same crash the plain KV path had before #6 guarded llm_graph_input_attn_kv::set_input.
Qwen3.5/3.8-class hybrids take this path. Same guard.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01FRQWzgSfo79JtnwZywHKrE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant