llama.cpp/src
Jonathan Graehl 5cdb27e091
finetune: SGD optimizer, more CLI args (#13873)
* examples/finetune -opt SGD (stochastic gradient descent) memory opt

add unit tested GGML_OPT_OPTIMIZER_SGD to ggml - avoids allocating
m, v tensors.

support finetune.cpp arg -opt SGD (or sgd). (default adamw as before)

llama 3.2-1b-F32 result: observed 11gb gpu ram (41 sec/epoch)
when using SGD instead of 19gb (55 sec/epoch) using adamw.
(wikipedia 100 lines finetune)

(
using the same GPU memory, adamw can only do before OOM 512
batch/context, reaching:
train: [███████▉] data=0000140/0000140 loss=0.02575±0.00099 acc=99.52±0.03% t=00:00:47 ETA=00:00:00
val:   [███████▉] data=0000008/0000008 loss=4.76565±0.28810 acc=41.46±0.77% t=00:00:00 ETA=00:00:00

SGD is superior, though it converges slower, with max before OOM 1728
batch/context (esp see the better validation perf):
train: [███████▉] data=0000039/0000039 loss=0.00371±0.00010 acc=99.96±0.01% t=00:00:41 ETA=00:00:00
val:   [███████▉] data=0000003/0000003 loss=5.11406±0.76034 acc=48.01±0.69% t=00:00:01 ETA=00:00:00
)

note: when finetuning long enough (or w/ enough -lr),
validation accuracy *eventually* drops ('catastrophic forgetting')

-lr-half (halflife) option useful for SGD to avoid oscillation or
super slow underdamped learning (makes setting -lr more forgiving).
terminal -lr for now is set by lr-halvings i.e. if you want at most
1/8 the inital -lr you set -lr-halvings 3.

note: objective loss not directly comparable between adamw, sgd? -
check perplexity or accuracy or consider relative improvements
for convergence

new finetune args -wd 1e-9 to enable weight decay in sgd or adamw,
and max -epochs N (default 2 as before)

cache (1 - wd*alpha) in 'adamw' opt struct -
no noticeable perf benefit, disabled (still done
for new SGD though)

since opt. memory is pre-allocated, the ggml_opt_get_optimizer_params
would probably be able to change between SGD and AdamW with each epoch
but would need to use adamw for the first (unconfirmed - no cmdline arg
to set such a policy yet)

test-opt checks adamw as before and now sgd (except for a few disabled
tests for sgd only; probably just needs logging values and adding
alternate reference values);  tolerance on the 'regression'
test is broader for sgd (so we don't need many more epochs)

* Vulkan: Implement GGML_OP_OPT_STEP_SGD

* tests: Fix OPT_STEP_SGD test-backend-ops

* SGD op param store weight-decay and not 1-alpha*wd

* minor + cosmetic changes

* fix vulkan sgd

* try CI fix

---------

Co-authored-by: 0cc4m <picard12@live.de>
Co-authored-by: Johannes Gäßler <johannesg@5d6.de>
2025-08-14 12:03:57 +02:00
..
CMakeLists.txt memory : Hybrid recurrent cache (#13979) 2025-06-19 08:08:14 +03:00
llama-adapter.cpp llama : do not crash if there is no CPU backend (#13395) 2025-05-09 13:02:07 +02:00
llama-adapter.h llama : refactor llama_context, llama_kv_cache, llm_build_context (#12181) 2025-03-13 12:35:44 +02:00
llama-arch.cpp llama : add gpt-oss (#15091) 2025-08-05 22:10:36 +03:00
llama-arch.h llama : add gpt-oss (#15091) 2025-08-05 22:10:36 +03:00
llama-batch.cpp tests : update for LLAMA_SET_ROWS=1 (#14961) 2025-07-30 15:12:02 +03:00
llama-batch.h llama : reuse compute graphs (#14482) 2025-07-17 19:08:33 +03:00
llama-chat.cpp chat : fix yandex chat template (#15116) 2025-08-06 13:26:49 +02:00
llama-chat.h llama : add gpt-oss (#15091) 2025-08-05 22:10:36 +03:00
llama-context.cpp finetune: SGD optimizer, more CLI args (#13873) 2025-08-14 12:03:57 +02:00
llama-context.h finetune: SGD optimizer, more CLI args (#13873) 2025-08-14 12:03:57 +02:00
llama-cparams.cpp cparams : rename LLAMA_MAX_PARALLEL_SEQUENCES to LLAMA_MAX_SEQ (#14188) 2025-06-15 10:08:58 +03:00
llama-cparams.h llama : add high-throughput mode (#14363) 2025-07-16 16:35:42 +03:00
llama-grammar.cpp server: streaming of tool calls and thoughts when --jinja is on (#12379) 2025-05-25 01:48:08 +01:00
llama-grammar.h tool-call: fix Qwen 2.5 Coder support, add micro benchmarks, support trigger patterns for lazy grammars (#12034) 2025-03-05 13:05:13 +00:00
llama-graph.cpp llama : add gpt-oss (#15091) 2025-08-05 22:10:36 +03:00
llama-graph.h llama : add gpt-oss (#15091) 2025-08-05 22:10:36 +03:00
llama-hparams.cpp model : add support for SmallThinker series (#14898) 2025-07-28 13:47:00 +02:00
llama-hparams.h llama : add gpt-oss (#15091) 2025-08-05 22:10:36 +03:00
llama-impl.cpp GGUF: C++ refactor, backend support, misc fixes (#11030) 2025-01-07 18:01:58 +01:00
llama-impl.h cleanup: fix compile warnings associated with gnu_printf (#11811) 2025-02-12 10:06:53 -04:00
llama-io.cpp llama : refactor llama_context, llama_kv_cache, llm_build_context (#12181) 2025-03-13 12:35:44 +02:00
llama-io.h llama : refactor llama_context, llama_kv_cache, llm_build_context (#12181) 2025-03-13 12:35:44 +02:00
llama-kv-cache-unified-iswa.cpp llama : add high-throughput mode (#14363) 2025-07-16 16:35:42 +03:00
llama-kv-cache-unified-iswa.h llama : add high-throughput mode (#14363) 2025-07-16 16:35:42 +03:00
llama-kv-cache-unified.cpp kv-cache : fix seq_rm with seq_id == -1 (#15226) 2025-08-11 13:58:24 +03:00
llama-kv-cache-unified.h llama : enable LLAMA_SET_ROWS=1 by default (#14959) 2025-08-02 17:14:21 +03:00
llama-kv-cells.h kv-cache : use ggml_set_rows (#14285) 2025-07-03 10:53:35 +03:00
llama-memory-hybrid.cpp memory : handle kv_unified for hybrid models (#15050) 2025-08-03 21:43:07 +02:00
llama-memory-hybrid.h memory : handle kv_unified for hybrid models (#15050) 2025-08-03 21:43:07 +02:00
llama-memory-recurrent.cpp memory : handle saving/loading null layers in recurrent memory (#14675) 2025-07-23 11:16:41 +03:00
llama-memory-recurrent.h memory : rename interface to llama_memory_context_i (#14296) 2025-06-21 08:03:46 +03:00
llama-memory.cpp memory : correctly handle failure in apply() (#14438) 2025-06-30 18:03:03 +03:00
llama-memory.h memory : correctly handle failure in apply() (#14438) 2025-06-30 18:03:03 +03:00
llama-mmap.cpp llama : allow using mmap without PrefetchVirtualMemory, apply GGML_WIN_VER to llama.cpp sources (#14013) 2025-06-05 11:57:42 +02:00
llama-mmap.h llama-mmap: fix missing include (#11796) 2025-02-10 20:58:18 +02:00
llama-model-loader.cpp llama : add gpt-oss (#15091) 2025-08-05 22:10:36 +03:00
llama-model-loader.h model: support GLM 4.5 family of models (#14939) 2025-08-04 20:29:25 +02:00
llama-model-saver.cpp llama : improve sep token handling (#14272) 2025-06-20 14:04:09 +02:00
llama-model-saver.h llama/ggml: add LLM training support (#10544) 2025-05-12 14:44:49 +02:00
llama-model.cpp llama : add gpt-oss (#15091) 2025-08-05 22:10:36 +03:00
llama-model.h llama : add gpt-oss (#15091) 2025-08-05 22:10:36 +03:00
llama-quant.cpp convert : support non-mxfp4 HF model (#15153) 2025-08-07 23:26:03 +02:00
llama-quant.h llama : refactor src/llama.cpp (#10902) 2025-01-03 10:18:53 +02:00
llama-sampling.cpp sampling : make sure samplers return at least 1 token (#13822) 2025-05-27 12:07:52 +03:00
llama-sampling.h llama : add llama_vocab, functions -> methods, naming (#11110) 2025-01-12 11:32:42 +02:00
llama-vocab.cpp llama : add gpt-oss (#15091) 2025-08-05 22:10:36 +03:00
llama-vocab.h model : add hunyuan dense (#14878) 2025-08-01 15:31:12 +02:00
llama.cpp llama : add thread safety test (#14035) 2025-06-16 08:11:43 -07:00
unicode-data.cpp server : better security control for public deployments (#9776) 2024-10-08 13:27:04 +02:00
unicode-data.h llama : reduce compile time and binary size (#9712) 2024-10-02 15:49:55 +02:00
unicode.cpp model : add Kimi-K2 support (#14654) 2025-07-15 21:54:22 +02:00
unicode.h model : add Kimi-K2 support (#14654) 2025-07-15 21:54:22 +02:00