File size: 4,139 Bytes
f9dff7a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | ## Converting LitGPT weights to Hugging Face Transformers
LitGPT weights need to be converted to a format that Hugging Face understands with a [conversion script](../litgpt/scripts/convert_lit_checkpoint.py) before our scripts can run.
We provide a helpful command to convert models LitGPT models back to their equivalent Hugging Face Transformers format:
```bash
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.
### Loading converted LitGPT checkpoints into transformers
For example,
```bash
cp checkpoints/repo_id/config.json converted/config.json
```
Then, you can load the checkpoint file in a Python session as follows:
```python
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:
```python
model = AutoModel.from_pretrained("online_repo_id", state_dict=state_dict)
```
### Merging LoRA weights
Please note that if you want to convert a model that has been finetuned using an adapter like LoRA, these weights should be [merged](../litgpt/scripts/merge_lora.py) to the checkpoint prior to converting.
```sh
litgpt merge_lora path/to/lora/checkpoint_dir
```
<br>
<br>
# A finetuning and conversion tutorial
This section contains a reproducible example for finetuning a LitGPT model and converting it back into a HF `transformer` model.
1. Download a model of interest:
For convenience, we first specify an environment variable (optional) to avoid copy and pasting the whole path:
```bash
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:
```bash
litgpt download $repo_id
```
2. Finetune the model:
```bash
export finetuned_dir=out/lit-finetuned-model
litgpt finetune_lora $repo_id \
--out_dir $finetuned_dir \
--train.epochs 1 \
--data Alpaca
```
3. 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.
```bash
litgpt merge_lora $finetuned_dir/final
```
4. Convert the finetuning model back into a HF format:
```bash
litgpt convert_from_litgpt $finetuned_dir/final/ out/hf-tinyllama/converted
```
5. Load the model into a `transformers` model:
```python
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)
```
## Using the LM Evaluation Harness
To evaluate LitGPT models, use the integrated evaluation utilities based on Eleuther AI's LM Evaluation Harness. For more information, please see the [evaluation](evaluation.md) documentation.
Alternatively, if you wish to use converted LitGPT models with the LM Evaluation Harness from [Eleuther AI's GitHub repository](https://github.com/EleutherAI/lm-evaluation-harness), you can use the following steps.
1. Follow the instructions above to load the model into a Hugging Face transformers model.
2. Create a `model.safetensor` file:
```python
model.save_pretrained("out/hf-tinyllama/converted/")
```
3. Copy the tokenizer files into the model-containing directory:
```bash
cp checkpoints/$repo_id/tokenizer* out/hf-tinyllama/converted
```
4. Run the evaluation harness, for example:
```bash
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
```
|