LitGPT weights need to be converted to a format that Hugging Face understands with a conversion script before our scripts can run.
We provide a helpful command to convert models LitGPT models back to their equivalent Hugging Face Transformers format:
litgpt convert_from_litgpt checkpoint_dir converted_dir
These paths are just placeholders, you will need to customize them based on which finetuning or pretraining command you ran and its configuration.
For example,
cp checkpoints/repo_id/config.json converted/config.json
Then, you can load the checkpoint file in a Python session as follows:
import torch
from transformers import AutoModel
state_dict = torch.load("output_dir/model.pth")
model = AutoModel.from_pretrained(
"output_dir/", local_files_only=True, state_dict=state_dict
)
Alternatively, you can also load the model without copying the config.json
file as follows:
model = AutoModel.from_pretrained("online_repo_id", state_dict=state_dict)
Please note that if you want to convert a model that has been finetuned using an adapter like LoRA, these weights should be merged to the checkpoint prior to converting.
litgpt merge_lora path/to/lora/checkpoint_dir
This section contains a reproducible example for finetuning a LitGPT model and converting it back into a HF transformer
model.
- Download a model of interest:
For convenience, we first specify an environment variable (optional) to avoid copy and pasting the whole path:
export repo_id=TinyLlama/TinyLlama-1.1B-intermediate-step-1431k-3T
Instead of using TinyLlama, you can replace the repo_id
target with any other model repository
specifier that is currently supported by LitGPT. You can get a list of supported repository specifier
by running litgpt/scripts/download.py
without any additional arguments.
Then, we download the model we specified via $repo_id
above:
litgpt download $repo_id
- Finetune the model:
export finetuned_dir=out/lit-finetuned-model
litgpt finetune_lora $repo_id \
--out_dir $finetuned_dir \
--train.epochs 1 \
--data Alpaca
- Merge LoRA weights:
Note that this step only applies if the model was finetuned with lora.py
above and not when full.py
was used for finetuning.
litgpt merge_lora $finetuned_dir/final
- Convert the finetuning model back into a HF format:
litgpt convert_from_litgpt $finetuned_dir/final/ out/hf-tinyllama/converted
- Load the model into a
transformers
model:
import torch
from transformers import AutoModel
state_dict = torch.load('out/hf-tinyllama/converted/model.pth')
model = AutoModel.from_pretrained("TinyLlama/TinyLlama-1.1B-intermediate-step-1431k-3T", state_dict=state_dict)
To evaluate LitGPT models, use the integrated evaluation utilities based on Eleuther AI's LM Evaluation Harness. For more information, please see the evaluation documentation.
Alternatively, if you wish to use converted LitGPT models with the LM Evaluation Harness from Eleuther AI's GitHub repository, you can use the following steps.
-
Follow the instructions above to load the model into a Hugging Face transformers model.
-
Create a
model.safetensor
file:
model.save_pretrained("out/hf-tinyllama/converted/")
- Copy the tokenizer files into the model-containing directory:
cp checkpoints/$repo_id/tokenizer* out/hf-tinyllama/converted
- Run the evaluation harness, for example:
lm_eval --model hf \
--model_args pretrained=out/hf-tinyllama/converted \
--tasks "hellaswag,gsm8k,truthfulqa_mc2,mmlu,winogrande,arc_challenge" \
--device "cuda:0" \
--batch_size 4