Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create fine-tune-LLM-with-Autotrain.md #516

Merged
merged 10 commits into from
Nov 4, 2024
100 changes: 100 additions & 0 deletions docs/sophia/data-science/fine-tune-LLM-with-Autotrain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# Autotrain

Autotrain, developed by Hugging Face, is a platform designed to simplify training cutting-edge models in various fields: NLP, LLM, CV, ... [read more](https://huggingface.co/docs/autotrain/main/en/tasks/llm_finetuning)

## Create Python Virtual Environment for Autotrain

Let's first create an environment for Autotrain:
felker marked this conversation as resolved.
Show resolved Hide resolved

```bash
mkdir -p venv_autotrain
python -m venv venv_autotrain --system-site-packages
felker marked this conversation as resolved.
Show resolved Hide resolved
source venv_autotrain/bin/activate
pip3 install autotrain-advanced
```

**Note:** If Autotrain doesn't work properly, you may have to reinstall `nvidia-ml-py`.

```bash
pip3 uninstall nvidia-ml-py3 pynvml
pip3 install --force-reinstall nvidia-ml-py==11.450.51
```

## Train Dataset Format

The dataset should have a column "text" containing the data to be trained on. [Example](https://huggingface.co/datasets/timdettmers/openassistant-guanaco)

## Config File for Fine-Tuning Local LLM

Here is an example to create a config file for supervised fine-tuning purposes:

```yaml
task: llm-sft
base_model: meta-llama/Meta-Llama-3.1-8B-Instruct
project_name: Llama-3-1-FT
log: wandb
backend: local
data:
path: Path/to/the/training/dataset/folder
train_split: train
valid_split: null
chat_template: null
column_mapping:
text_column: text
params:
block_size: 1024
model_max_length: 8192
epochs: 800
batch_size: 2
lr: 1e-5
peft: true
quantization: null
target_modules: all-linear
padding: right
optimizer: paged_adamw_8bit
scheduler: cosine
gradient_accumulation: 8
mixed_precision: bf16
hub:
username: ***
token: hf_***
push_to_hub: true
```

[More details](https://huggingface.co/docs/autotrain/en/config)

## Run Autotrain to Fine-Tune Using the Config File

```bash
cd Path/to/save/the/adapter
autotrain --config path/to/config.yaml
```

## Merge Adapters with Base Model to Create New Model

Adapters need to be merged with the base model in order to run. You can use the code below:

```python
from transformers import AutoModelForCausalLM, AutoTokenizer, AutoConfig
import torch
from peft import PeftModel
import os

adapter = "path/to/saved/adapters/"
model_name = "project-name-from-config-file"
adapter_path = os.path.join(adapter, model_name)
base_model_path = "meta-llama/Meta-Llama-3.1-8B-Instruct"
target_model_path = "path/to/save/fine-tuned/models" + model_name

config = AutoConfig.from_pretrained(base_model_path)
base_model = AutoModelForCausalLM.from_pretrained(base_model_path)

merged_model = PeftModel.from_pretrained(base_model, adapter_path)

tokenizer = AutoTokenizer.from_pretrained(adapter_path, trust_remote_code=True)
merged_model = merged_model.merge_and_unload()

print("Saving target model...")
merged_model.save_pretrained(target_model_path)
tokenizer.save_pretrained(target_model_path)
config.save_pretrained(target_model_path)
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ nav:
- Containers: sophia/containers/containers.md
- Data Science:
- Python: sophia/data-science/python.md
- Fine-tuning with Autotrain: sophia/data-science/fine-tune-LLM-with-Autotrain.md
- Visualization:
- Visualization on Sophia: sophia/visualization/visualization.md
- ParaView (Launch from Client): sophia/visualization/paraview.md
Expand Down
Loading