llama.cpp/examples/finetune
compilade 3fd62a6b1c
py : type-check all Python scripts with Pyright (#8341)
* py : type-check all Python scripts with Pyright

* server-tests : use trailing slash in openai base_url

* server-tests : add more type annotations

* server-tests : strip "chat" from base_url in oai_chat_completions

* server-tests : model metadata is a dict

* ci : disable pip cache in type-check workflow

The cache is not shared between branches, and it's 250MB in size,
so it would become quite a big part of the 10GB cache limit of the repo.

* py : fix new type errors from master branch

* tests : fix test-tokenizer-random.py

Apparently, gcc applies optimisations even when pre-processing,
which confuses pycparser.

* ci : only show warnings and errors in python type-check

The "information" level otherwise has entries
from 'examples/pydantic_models_to_grammar.py',
which could be confusing for someone trying to figure out what failed,
considering that these messages can safely be ignored
even though they look like errors.
2024-07-07 15:04:39 -04:00
..
CMakeLists.txt build: rename main → llama-cli, server → llama-server, llava-cli → llama-llava-cli, etc... (#7809) 2024-06-13 00:41:52 +01:00
convert_finetune_checkpoint_to_gguf.py py : type-check all Python scripts with Pyright (#8341) 2024-07-07 15:04:39 -04:00
finetune.cpp ggml : refactor rope norm/neox (#7634) 2024-06-05 11:29:20 +03:00
finetune.sh finetune: Rename an old command name in finetune.sh (#8344) 2024-07-07 13:37:47 +03:00
README.md finetune: Rename command name in README.md (#8343) 2024-07-07 13:38:02 +03:00

finetune

Basic usage instructions:

# get training data
wget https://raw.githubusercontent.com/brunoklein99/deep-learning-notes/master/shakespeare.txt

# finetune LORA adapter
./bin/llama-finetune \
        --model-base open-llama-3b-v2-q8_0.gguf \
        --checkpoint-in  chk-lora-open-llama-3b-v2-q8_0-shakespeare-LATEST.gguf \
        --checkpoint-out chk-lora-open-llama-3b-v2-q8_0-shakespeare-ITERATION.gguf \
        --lora-out lora-open-llama-3b-v2-q8_0-shakespeare-ITERATION.bin \
        --train-data "shakespeare.txt" \
        --save-every 10 \
        --threads 6 --adam-iter 30 --batch 4 --ctx 64 \
        --use-checkpointing

# predict
./bin/llama-cli -m open-llama-3b-v2-q8_0.gguf --lora lora-open-llama-3b-v2-q8_0-shakespeare-LATEST.bin

Only llama based models are supported! The output files will be saved every N iterations (config with --save-every N). The pattern 'ITERATION' in the output filenames will be replaced with the iteration number and with 'LATEST' for the latest output. So in above example after 10 iterations these files will be written:

  • chk-lora-open-llama-3b-v2-q8_0-shakespeare-10.gguf
  • chk-lora-open-llama-3b-v2-q8_0-shakespeare-LATEST.gguf
  • lora-open-llama-3b-v2-q8_0-shakespeare-10.bin
  • lora-open-llama-3b-v2-q8_0-shakespeare-LATEST.bin

After 10 more iterations:

  • chk-lora-open-llama-3b-v2-q8_0-shakespeare-20.gguf
  • chk-lora-open-llama-3b-v2-q8_0-shakespeare-LATEST.gguf
  • lora-open-llama-3b-v2-q8_0-shakespeare-20.bin
  • lora-open-llama-3b-v2-q8_0-shakespeare-LATEST.bin

Checkpoint files (--checkpoint-in FN, --checkpoint-out FN) store the training process. When the input checkpoint file does not exist, it will begin finetuning a new randomly initialized adapter.

llama.cpp compatible LORA adapters will be saved with filename specified by --lora-out FN. These LORA adapters can then be used by llama-cli together with the base model, like in the 'predict' example command above.

In llama-cli you can also load multiple LORA adapters, which will then be mixed together.

For example if you have two LORA adapters lora-open-llama-3b-v2-q8_0-shakespeare-LATEST.bin and lora-open-llama-3b-v2-q8_0-bible-LATEST.bin, you can mix them together like this:

./bin/llama-cli -m open-llama-3b-v2-q8_0.gguf \
  --lora lora-open-llama-3b-v2-q8_0-shakespeare-LATEST.bin \
  --lora lora-open-llama-3b-v2-q8_0-bible-LATEST.bin

You can change how strong each LORA adapter is applied to the base model by using --lora-scaled FN SCALE instead of --lora FN.

For example to apply 40% of the 'shakespeare' LORA adapter, 80% of the 'bible' LORA adapter and 100% of yet another one:

./bin/llama-cli -m open-llama-3b-v2-q8_0.gguf \
  --lora-scaled lora-open-llama-3b-v2-q8_0-shakespeare-LATEST.bin 0.4 \
  --lora-scaled lora-open-llama-3b-v2-q8_0-bible-LATEST.bin 0.8 \
  --lora lora-open-llama-3b-v2-q8_0-yet-another-one-LATEST.bin

The scale numbers don't need to add up to one, and you can also use numbers greater than 1 to further increase the influence of an adapter. But making the values too big will sometimes result in worse output. Play around to find good values.

Gradient checkpointing reduces the memory requirements by ~50% but increases the runtime. If you have enough RAM, you can make finetuning a bit faster by disabling checkpointing with --no-checkpointing.

The default LORA rank can be specified with --lora-r N. The LORA rank can be configured for each model tensor type separately with these command line options:

  --lora-r N                 LORA r: default rank. Also specifies resulting scaling together with lora-alpha. (default 4)
  --rank-att-norm N          LORA rank for attention norm tensor (default 1)
  --rank-ffn-norm N          LORA rank for feed-forward norm tensor (default 1)
  --rank-out-norm N          LORA rank for output norm tensor (default 1)
  --rank-tok-embd N          LORA rank for token embeddings tensor (default 4)
  --rank-out N               LORA rank for output tensor (default 4)
  --rank-wq N                LORA rank for wq tensor (default 4)
  --rank-wk N                LORA rank for wk tensor (default 4)
  --rank-wv N                LORA rank for wv tensor (default 4)
  --rank-wo N                LORA rank for wo tensor (default 4)
  --rank-ffn_gate N          LORA rank for ffn_gate tensor (default 4)
  --rank-ffn_down N          LORA rank for ffn_down tensor (default 4)
  --rank-ffn_up N            LORA rank for ffn_up tensor (default 4)

The LORA rank of 'norm' tensors should always be 1.

To see all available options use llama-finetune --help.