Skip to main content

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.

Metadata

Release files for caft-llm 0.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for caft-llm 0.0.1
File Size Uploaded Provenance
caft_llm-0.0.1.tar.gz 17.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for caft-llm 0.0.1
File Interpreter ABI Platform
caft_llm-0.0.1-py3-none-any.whl Python 3 none any Details

Total release size: 30.4 kB

Release files / caft_llm-0.0.1.tar.gz

Download URL caft_llm-0.0.1.tar.gz
Size 17.9 kB
Tags Source
SHA-256 checksum
How to use checksums
11a0657f6b1750d12b41c0afb3cff4ee105fb9686f2f7f1f94910eac510ccc0f
BLAKE2b-256 checksum
How to use checksums
376b661db5123261ddc5e8c6ac927cd03e4d16f26161c24b61daa0bc993c0d61
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.13

Release files / caft_llm-0.0.1-py3-none-any.whl

Download URL caft_llm-0.0.1-py3-none-any.whl
Size 12.5 kB
Tags Python 3
SHA-256 checksum
How to use checksums
fad09667e0d31a6ffe62a785111670454d130124abd8964237324d7293690207
BLAKE2b-256 checksum
How to use checksums
ca068e83e78a9dc836617c34e373423841796e99c5ca7ffbf075974eb4ca8376
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.9.13

Security for caft-llm 0.0.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.1 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page