Skip to main content

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

  1. Create .env file with HUGGINGFACE_TOKEN=<token> and optionally WANDB_TOKEN=<token>
  2. Add train_set.jsonl and eval_set.jsonl files to scripts/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 only meta-llama/Llama-3.1-8B-Instruct is supported.
  • --model-max-length -maxlen
  • --finetune-method -ftm: lora or sft (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-heads must also be set to True.

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

  1. Download the train and validation dataset from this Huggingface repo and save to scripts/datasets
  2. 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

caft_llm-0.0.1.tar.gz (17.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

caft_llm-0.0.1-py3-none-any.whl (12.5 kB view details)

Uploaded Python 3

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

Hashes for caft_llm-0.0.1.tar.gz
Algorithm Hash digest
SHA256 11a0657f6b1750d12b41c0afb3cff4ee105fb9686f2f7f1f94910eac510ccc0f
MD5 2c9ebc686bfca03abb95b53d3f45a6b7
BLAKE2b-256 376b661db5123261ddc5e8c6ac927cd03e4d16f26161c24b61daa0bc993c0d61

See more details on using hashes here.

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

Hashes for caft_llm-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fad09667e0d31a6ffe62a785111670454d130124abd8964237324d7293690207
MD5 214da4f77c44bb60c0942b9b82230830
BLAKE2b-256 ca068e83e78a9dc836617c34e373423841796e99c5ca7ffbf075974eb4ca8376

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page