Skip to content

Latest commit

 

History

History
338 lines (253 loc) · 11.2 KB

ml.md

File metadata and controls

338 lines (253 loc) · 11.2 KB

ML Training Details

Native LoRA/QLoRA training for GGUF models in llama.cpp-ML.

Architecture

  • GGUF frozen base
    • LoRA A/B trainable tensors
    • Forward through base → backward into LoRA tensors
    • Optimizer (CPU or CUDA)
    • Adapter save → reload → delta verification → logits effect

The base GGUF model remains frozen and quantized. Only LoRA A/B adapter tensors are trainable in FP16/FP32. Trained adapters save as GGUF files compatible with the existing llama.cpp LoRA loader.

Models Used

Model Quant Size Purpose
stories15M F32 15M params Fast smoke tests, full q/k/v/o validation
Qwen3-4B F16 4B params CUDA performance baseline, F16 validation
Qwen3-4B Q4_K_M 4B params QLoRA validation, VRAM reduction
Qwen3-1.7B F16 1.7B params CPU/Android validation, late-layer targets
Qwen3-1.7B Q4_K_M 1.7B params Android QLoRA validation
Qwen3-8B Q4_K_M 8B params Large model Q4 training (early stage)

Datasets

Dataset Format Domain Records Use
stories15M flat tokens Story completion 256 CPU/CUDA smoke, early validation
OPUS EN→PL JSONL SFT English→Polish translation 256–1024 Main training corpus
Tatoeba EN→PL JSONL SFT English→Polish translation 256–1024 Mixed with OPUS for robustness

The opus_to_sft_jsonl.py script converts OPUS NDJSON (input/output pairs) into SFT messages JSONL with system prompt "Translate English to Polish."

Dataset Types

  • Clean: OPUS-only EN→PL translation pairs
  • Mixed: 80/20 OPUS + Tatoeba blend (mixed_80_20)

LoRA Targets

Preset Targets Layer Notes
late-attn-q blk.N.attn_q.weight Last layer Fastest, short backward path
late-attn-qv blk.N.attn_q.weight + blk.N.attn_v.weight Last layer 2 targets
late-attn-qkvo blk.N.q/k/v/o Last layer Full attention, practical
late4-attn-qkvo blk.(N-3)–N q/k/v/o 4 layers 16 targets, longer backward

For Qwen3-4B: last usable layer = blk.35 (36 layers total). For Qwen3-1.7B: last usable layer = blk.27 (28 layers total).

Linux CPU Tests

stories15M – Full q/k/v/o (v1.16)

Metric Result
Model stories15M.gguf (F32)
Backend CPU
Targets q/k/v/o (all attention)
Adapter update PASS
Save/reload PASS
Logits effect PASS (changed=151933)
No GGML_ASSERT PASS

Conclusion: First multi-target LoRA training on CPU. Stories15M validates the full q/k/v/o pipeline.

Qwen3-4B F16 – Single target CPU (v1.21)

Metric Result
Model Qwen3-4B-f16.gguf
Backend CPU
Target blk.0.attn_q.weight
Train loss 1.134492310e+00 (finite=1)
Eval loss 2.800914204e-01 (finite=1)
Adapter update PASS
Save/reload PASS (exact reload match)
Logits effect PASS (changed=151933)

Conclusion: First stable Qwen CPU reference checkpoint. GGUF LoRA training without PyTorch works on Linux CPU.

Qwen3-4B F16 – CPU vs CUDA correctness (v1.22)

Metric CPU (v1.21) CUDA (v1.22)
Train loss 1.134492310e+00 1.133795821e+00
Eval loss 2.800914204e-01 2.795151779e-01
Logits changed 151933 151936

Conclusion: CPU and CUDA produce numerically matching results. CPU remains a correctness reference.

CPU Scaling Tests (v1.35.x)

Host: Ryzen 9 5950X, 32 threads. Model: Qwen3-1.7B Q4_K_M (short profiles) and Qwen3-4B Q4_K_M.

Thread propagation (v1.35.0)

All thread counts propagate correctly through the training graph:

Threads Effective Batch
t1 1 1
t4 4 4
t8 8 8
t16 16 16
t32 32 32

CPU saturation — Qwen3-1.7B Q4 (v1.35.2)

Metric Value
wall time 29.9 s
process CPU time 900.7 s
cpu_parallelism 30.1x
CPU utilization 2760% (27 cores equivalent of 32 available)
opt_eval per step ~1.7–1.9 s
Correctness PASS

CPU saturation — Qwen3-4B Q4 clean (v1.35.3)

Metric Value
wall time 123.0 s
process CPU time 3290.1 s
cpu_parallelism 26.7x
CPU utilization 2612%
Train loss 1.062842941e+01 (finite=1)
Adapter update PASS

CPU saturation — Qwen3-4B Q4 mixed (v1.35.3)

Metric Value
wall time 93.8 s
process CPU time 2815.0 s
cpu_parallelism 30.0x
CPU utilization 2869%
Train loss 8.162411138e+00 (finite=1)
Adapter update PASS

Conclusion: CPU scaling works on short profiles (4–8 windows, n_ctx=256). Threading reaches ~27–30x parallelism on Ryzen 9 5950X with full CPU saturation (~2700% CPU). For large training profiles (stories15M, 7M+ records), CPU throughput is ~148x lower than CUDA (15.4 vs 2282 tokens/s; wall time ~96 min vs ~53 min, ~1.8x), so CPU remains a correctness/regression path for full-scale training.

Linux CUDA Tests

Qwen3-4B F16 – CUDA-min correctness (v1.22)

Metric CPU (v1.21) CUDA (v1.22)
Train loss 1.134492310e+00 1.133795821e+00
Eval loss 2.800914204e-01 2.795151779e-01
Logits changed 151933 151936
Adapter update PASS PASS
Save/reload PASS PASS

Conclusion: CUDA correctness validated against CPU reference. Loss and logits match within numerical precision.

CUDA Performance Bottleneck – Target Depth (v1.25.5)

Target Epoch Train Time Ratio vs blk.0
blk.0.attn_q.weight 340.4 s 1x (baseline)
blk.35.attn_q.weight 39.1 s 8.7x faster

Conclusion: Early-layer targets produce long backward paths through all 36 transformer layers. Late-layer targets are ~8.7x faster. Late-layer presets (blk.35) enable practical CUDA training.

CUDA Late-layer Multi-target (v1.25.7)

Preset Targets Epoch Train Time
late-attn-q blk.35.q ~35 s
late-attn-qv blk.35.q + blk.35.v 53.0 s
late-attn-qkvo blk.35.q/k/v/o 42.2 s

Conclusion: All multi-target presets PASS. The full q/k/v/o preset remains practical on CUDA.

CUDA OUT_PROD Fix – F16→F32 Staging (v1.26.12)

Before fix (v1.26.10):

  • src1 activation OUT_PROD: total=2, cuda=0, host=2 (CPU fallback)
  • Bottleneck: activation-gradient OUT_PROD falling back to CUDA_Host

After fix (v1.26.12):

  • src1 activation OUT_PROD: total=2, cuda=2, host=0
  • Path: F16 frozen weight → temporary F32 staging on CUDA → F32 OUT_PROD on CUDA
  • epoch_train: 0.897 s (was 340.4 s for early target)

Performance:

Stage Time
Total 2.801 s
Epoch train 0.897 s
Save 0.507 ms
Reload 0.335 ms
Logits eval 46.4 ms

Conclusion: CUDA OUT_PROD bottleneck fixed. Required activation-gradient nodes stay on CUDA.

CUDA Stability Soak (v1.26.13)

52-minute CUDA run:

Metric Value
Total time 3152.8 s (~52 min 33 s)
Train data 7181312 records
Train loss 4.102389934e-01 (finite=1)
Eval loss 3.891401536e+00 (finite=1)
Adapter update PASS (4/4)
Reload PASS (4/4)
Logits changed 151936
Avg GPU utilization 84.2%
Avg VRAM 11211 MiB
Avg power 242.3 W

Conclusion: CUDA training remains stable for extended runs. GPU utilization confirms real CUDA work.

JSONL / SFT Dataset Pipeline

SFT Masking (v1.28.x)

Metric Value
Messages records 256
Assistant messages (trainable) 256
Ignored messages (system/user) 512
Train tokens 3218
Ignored tokens 5266
Train ratio 0.379302 (38%)

Full OPUS/Tatoeba SFT Profile (v1.30.0)

Metric Value
Tokens 147713
Active labels 69744
Ignored labels 77969
Active ratio 0.472159
Dataset windows 576
Train/eval split 512 / 64
Empty windows 0
Low-density windows 0
Avg active per window 120.884 / 256

Q4/QLoRA F16 Baseline Comparison (v1.31.0 vs v1.32.4)

Metric F16 q/k/v/o (v1.31.0) Q4 q/k/v/o (v1.32.4)
Epoch train 57.99 s 43.94 s
Train loss 6.239481111e+00 6.307221476e+00
Eval loss 4.569893531e+00 4.697681928e+00
Adapter update PASS PASS
Save/reload PASS PASS
Logits changed 151936 151936
Delta sum abs 1.643133410e+06 1.680395833e+06

Q4 speedup: ~24% vs F16 for q/k/v/o targets on RTX 4090.

Q4 OUT_PROD Fix (v1.32.2)

Before fix (v1.32.1):

  • OUT_PROD: 5 CUDA / 5 host
  • epoch_train: 73.2 s

After fix (v1.32.2):

  • OUT_PROD: 10 CUDA / 0 host
  • epoch_train: 0.876 s
  • Speedup: ~83x on short diagnostic test

Qwen3-1.7B Q4 CPU Deep Target (v1.36.1)

Metric Value
Model Qwen3-1.7B-Q4_K_M.gguf
Backend CPU
Target blk.27.attn_q.weight
Train records 32/32
Train loss 5.73805 (finite=1)
Adapter update PASS
Save/reload PASS
Logits changed 151935
Delta sum abs 5.984148462e+03
Total time 404.6 s
Epoch train 393.3 s

Quality Evaluation

Qwen3-4B Q4 – Clean EN→PL 300 prompts (v1.33.6)

Metric Base Model LoRA Adapter (512 windows)
Exact match 5 10 (+100%)
Norm exact 5 11 (+120%)
Empty outputs 0 1
BLEU-1 lite 0.179786 0.290465 (+62%)
chrF lite 0.552903 0.607359 (+10%)
Avg chars 44.80 42.26

Qwen3-4B Q4 – Mixed 32 from Clean (v1.34.1)

Clean EN→PL improvement:

Metric Before (clean) After (mixed+32)
BLEU-1 lite 0.290465 0.302303 (+4%)
chrF lite 0.607359 0.619582 (+2%)

Correct-polish improvement:

Metric Before (clean) After (mixed+32)
BLEU-1 lite 0.450364 0.462482 (+3%)
chrF lite 0.714777 0.723724 (+1%)

Mixed Dose Results Summary

Dose Clean EN→PL Correct-polish Verdict
Mixed 640 windows FAIL (regression) N/A Rejected
Mixed 128 windows FAIL (regression) FAIL Rejected
Mixed 32 windows PASS (+4% BLEU) PASS (+3% BLEU) Accepted

Best confirmed adapter: train_lora_mixed_32_from_clean_1_34_1_qwen_q4_cuda.gguf

Limitations

  • Quality evaluation is prepared but not fully automated
  • Full checkpoint/resume (optimizer, scheduler, RNG state) is not complete
  • CPU training is functionally correct and scales well on short profiles (~30x parallelism on Ryzen 9 5950X), but in the measured large profile CUDA delivered ~148x higher throughput than CPU (2282 vs 15.4 tokens/s; wall time ~96 min vs ~53 min, ~1.8x). CPU therefore remains a correctness/regression path for full-scale training
  • Not upstream-ready without cleanup
  • Mixed training is sensitive to dose; 32 windows is the only confirmed safe dose

Version Notes

Version markers such as v1.35.4, v1.36.1, or v1.34.1 refer to internal experimental milestones from the development tree. They are kept in this document to preserve the validation history and debugging timeline.

All validated work described here is consolidated into the public v0.1.0 release.