No project description provided
Project description
Concept-Aware Fine-tuning (CAFT)
Concept-aware fine-tuning (CAFT) encourages stronger conceptual understanding by incorporating multi-token prediction into fine-tuning.
Installation
git clone https://github.com/michaelchen-lab/caft-llm.git
cd caft-llm
pip install -e .
Setup
- Create
.envfile withHUGGINGFACE_TOKEN=<token>and optionallyWANDB_TOKEN=<token> - Add
train_set.jsonlandeval_set.jsonlfiles toscripts/datasets/. Each instance should be of the format:
{
"id": "<int/str>", "status": "OK",
"conversation": [
{"role": "human", "content": "(prompt)"},
{"role": "assistant", "content": "(ground truth answer)"},
]
}
Fine-tune a model using CAFT
Currently, only the auxiliary heads of meta-llama/Llama-3.1-8B-Instruct have been pretrained.
Method 1: Use the provided training script scripts/train.py
torchrun --nprod-per-node 1 scripts/train.py -ftm lora
torchrun --nprod-per-node 1 scripts/train.py -ftm lora -ft-heads -hpretrain
torchrun --nprod-per-node 1 scripts/train.py -ftm sft -lr 5e-6 -fr-unembed
torchrun --nprod-per-node 1 scripts/train.py -ftm sft -lr 5e-6 -fr-unembed -ft-heads -hpretrain
Selected Arguments:
--model-name-or-path -model: Currently onlymeta-llama/Llama-3.1-8B-Instructis supported.--model-max-length -maxlen--finetune-method -ftm:loraorsft(full finetuning)--learning-rate -lr--epochs -e--freeze-unembedding -fr-unembed: Only applicable for full fine-tuning. Recommended:True--per-device-batch-size -micro-bs--gradient-accumulation-steps -grad-acc--heads-pretraining -hpretrain: Train auxiliary heads on your dataset for 1 epoch before apply CAFT to your model.-ft-headsmust also be set toTrue.
The full list of arguments can be found using this command:
python scripts/train.py --help
Method 2: Integrate CAFT into your existing Transformers fine-tuning pipeline
import transformers
from caft import *
# Import your pretrained Transformers model, tokenizer, TrainingArguments, and data_module
add_auxiliary_heads(model)
add_caft_loss(transformers)
trainer = transformers.trainer.Trainer( # The additional CAFT functions track and save the auxiliary losses
model=model, tokenizer=tokenizer, args=model_training_args,
callbacks=[CAFTSaveLogging],
compute_metrics=caft_compute_metrics,
preprocess_logits_for_metrics=preprocess_logits_for_metrics,
**data_module
)
Please refer to scripts/train.py for a complete implementation example.
(Optional) Train Auxiliary Heads
- Download the train and validation dataset from this Huggingface repo and save to
scripts/datasets - Run the following command
torchrun nproc-per-node 4 scripts/train_aux_heads.py
Contributing
We welcome community contributions to caft-llm. Feel free to open an issue or submit a pull request. If you have any questions or wish to collaborate, please contact michaelchenkj@gmail.com.
Acknowledgements
This codebase adapts code from several amazing projects, including Medusa and Facebook Multi-Token.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file caft_llm-0.0.1.tar.gz.
File metadata
- Download URL: caft_llm-0.0.1.tar.gz
- Upload date:
- Size: 17.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
11a0657f6b1750d12b41c0afb3cff4ee105fb9686f2f7f1f94910eac510ccc0f
|
|
| MD5 |
2c9ebc686bfca03abb95b53d3f45a6b7
|
|
| BLAKE2b-256 |
376b661db5123261ddc5e8c6ac927cd03e4d16f26161c24b61daa0bc993c0d61
|
File details
Details for the file caft_llm-0.0.1-py3-none-any.whl.
File metadata
- Download URL: caft_llm-0.0.1-py3-none-any.whl
- Upload date:
- Size: 12.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fad09667e0d31a6ffe62a785111670454d130124abd8964237324d7293690207
|
|
| MD5 |
214da4f77c44bb60c0942b9b82230830
|
|
| BLAKE2b-256 |
ca068e83e78a9dc836617c34e373423841796e99c5ca7ffbf075974eb4ca8376
|