Skip to content

Commit

Permalink
Merge pull request #516 from btrungvo/btrungvo-sophia
Browse files Browse the repository at this point in the history
Create fine-tune-LLM-with-Autotrain.md
  • Loading branch information
felker authored Nov 4, 2024
2 parents 57e4cfa + a4e102d commit a55ee6e
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 0 deletions.
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 a virtual environment for Autotrain, built on top of the minimal system Python installation located at `/usr/bin/python`:

```bash
mkdir -p venv_autotrain
python -m venv venv_autotrain --system-site-packages
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

0 comments on commit a55ee6e

Please sign in to comment.