Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ Our package has been fully tested on Linux OS (Ubuntu 20.04). Other OS platforms
You may encounter some unexpected errors. You may try it first on a Linux machine or use Google Colab to experience it.

```bash
git clone -b v0.0.4 https://github.com/OptimalScale/LMFlow.git
git clone -b v0.0.5 https://github.com/OptimalScale/LMFlow.git
cd LMFlow
conda create -n lmflow python=3.9 -y
conda activate lmflow
Expand All @@ -101,19 +101,36 @@ Please refer to our [doc](https://optimalscale.github.io/LMFlow/examples/DATASET
Full training updates all the parameters to finetune a language model.
Here is an example to finetune a GPT-2 base model
```sh
./scripts/run_finetune.sh
cd data && ./download.sh alpaca && cd -

./scripts/run_finetune.sh \
--model_name_or_path gpt2 \
--dataset_path data/alpaca/train \
--output_model_path output_models/finetuned_gpt2
```

### Finetuning (LoRA)
LoRA is a parameter-efficient finetuning algorithm and is more efficient than full finetuning.
```sh
./scripts/run_finetune_with_lora.sh
cd data && ./download.sh alpaca && cd -

# Saves lora only
./scripts/run_finetune_with_lora.sh \
--model_name_or_path facebook/galactica-1.3b \
--dataset_path data/alpaca/train \
--output_lora_path output_models/finetuned_galactica_lora

# Saves lora and merges into original model
./scripts/run_finetune_with_lora_save_aggregated_weights.sh \
--model_name_or_path facebook/galactica-1.3b \
--dataset_path data/alpaca/train \
--output_model_path output_models/finetuned_galactica
```

### Inference
After finetuning, you can run the following command to chat with the model.
```sh
./scripts/run_chatbot.sh {finetuned-checkpoints-path}
./scripts/run_chatbot.sh output_models/finetuned_gpt2
```

### Deployment
Expand Down
45 changes: 33 additions & 12 deletions scripts/run_finetune.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,47 @@
# https://github.com/shizhediao/llm-ft
# COMMIT: d5fecf30ba8011067b10cf51fede53a5ab6574e4

deepspeed_args="--master_port=11000" # Default argument
if [ $# -ge 1 ]; then
deepspeed_args="$1"
fi
# Parses arguments
model_name_or_path=gpt2
dataset_path=data/alpaca/train
output_dir=output_models/finetune
deepspeed_args="--master_port=11000"

while [[ $# -ge 1 ]]; do
key="$1"
case ${key} in
-m|--model_name_or_path)
model_name_or_path="$2"
shift
;;
-d|--dataset_path)
dataset_path="$2"
shift
;;
-o|--output_model_path)
output_dir="$2"
shift
;;
--deepspeed_args)
deepspeed_args="$2"
shift
;;
*)
echo "error: unknown option \"${key}\"" 1>&2
exit 1
esac
shift
done

# Finetune
exp_id=finetune
project_dir=$(cd "$(dirname $0)"/..; pwd)
output_dir=${project_dir}/output_models/${exp_id}
log_dir=${project_dir}/log/${exp_id}

dataset_path=${project_dir}/data/alpaca/train
if [ ! -d ${dataset_path} ]; then
cd data && ./download.sh alpaca && cd -
fi

mkdir -p ${output_dir} ${log_dir}

deepspeed ${deepspeed_args} \
examples/finetune.py \
--model_name_or_path gpt2 \
--model_name_or_path ${model_name_or_path} \
--dataset_path ${dataset_path} \
--output_dir ${output_dir} --overwrite_output_dir \
--num_train_epochs 0.01 \
Expand Down
47 changes: 34 additions & 13 deletions scripts/run_finetune_with_lora.sh
Original file line number Diff line number Diff line change
@@ -1,26 +1,47 @@
#!/bin/bash
# Please run this script under ${project_id} in project directory of

deepspeed_args="--master_port=11000" # Default argument
if [ $# -ge 1 ]; then
deepspeed_args="$1"
fi
# Parses arguments
model_name_or_path=gpt2
dataset_path=data/alpaca/train
output_dir=output_models/finetune
deepspeed_args="--master_port=11000"

while [[ $# -ge 1 ]]; do
key="$1"
case ${key} in
-m|--model_name_or_path)
model_name_or_path="$2"
shift
;;
-d|--dataset_path)
dataset_path="$2"
shift
;;
-o|--output_lora_path)
output_dir="$2"
shift
;;
--deepspeed_args)
deepspeed_args="$2"
shift
;;
*)
echo "error: unknown option \"${key}\"" 1>&2
exit 1
esac
shift
done

# Finetune
exp_id=finetune_with_lora
project_dir=$(cd "$(dirname $0)"/..; pwd)
output_dir=${project_dir}/output_models/${exp_id}
log_dir=${project_dir}/log/${exp_id}

dataset_path=${project_dir}/data/alpaca/train
if [ ! -d ${dataset_path} ]; then
cd data && ./download.sh alpaca && cd -
fi

mkdir -p ${output_dir} ${log_dir}

deepspeed ${deepspeed_args} \
examples/finetune.py \
--model_name_or_path facebook/galactica-1.3b \
--model_name_or_path ${model_name_or_path} \
--dataset_path ${dataset_path} \
--output_dir ${output_dir} --overwrite_output_dir \
--num_train_epochs 0.01 \
Expand All @@ -32,7 +53,7 @@ deepspeed ${deepspeed_args} \
--save_aggregated_lora 0\
--deepspeed configs/ds_config_zero2.json \
--fp16 \
--run_name finetune_with_lora \
--run_name ${exp_id} \
--validation_split_percentage 0 \
--logging_steps 20 \
--do_train \
Expand Down
52 changes: 34 additions & 18 deletions scripts/run_finetune_with_lora_save_aggregated_weights.sh
Original file line number Diff line number Diff line change
@@ -1,27 +1,47 @@
#!/bin/bash
# Please run this script under ${project_id} in project directory of

deepspeed_args="--master_port=11000" # Default argument
if [ $# -ge 1 ]; then
deepspeed_args="$1"
fi
# Parses arguments
model_name_or_path=gpt2
dataset_path=data/alpaca/train
output_dir=output_models/finetune
deepspeed_args="--master_port=11000"

while [[ $# -ge 1 ]]; do
key="$1"
case ${key} in
-m|--model_name_or_path)
model_name_or_path="$2"
shift
;;
-d|--dataset_path)
dataset_path="$2"
shift
;;
-o|--output_model_path)
output_dir="$2"
shift
;;
--deepspeed_args)
deepspeed_args="$2"
shift
;;
*)
echo "error: unknown option \"${key}\"" 1>&2
exit 1
esac
shift
done

# Finetune
exp_id=finetune_with_lora
project_dir=$(cd "$(dirname $0)"/..; pwd)
output_dir=${project_dir}/output_models/${exp_id}
log_dir=${project_dir}/log/${exp_id}

dataset_path=${project_dir}/data/alpaca/train
eval_dataset_path=${project_dir}/data/alpaca/test
if [ ! -d ${dataset_path} ]; then
cd data && ./download.sh alpaca && cd -
fi

mkdir -p ${output_dir} ${log_dir}

deepspeed ${deepspeed_args} \
examples/finetune.py \
--model_name_or_path facebook/galactica-1.3b \
--model_name_or_path ${model_name_or_path} \
--dataset_path ${dataset_path} \
--output_dir ${output_dir} --overwrite_output_dir \
--num_train_epochs 0.01 \
Expand All @@ -33,14 +53,10 @@ deepspeed ${deepspeed_args} \
--save_aggregated_lora 1\
--deepspeed configs/ds_config_zero2.json \
--fp16 \
--run_name finetune_with_lora \
--run_name ${exp_id} \
--validation_split_percentage 0 \
--logging_steps 20 \
--do_train \
--do_eval \
--evaluation_strategy "steps" \
--eval_steps 1000 \
--eval_dataset_path ${eval_dataset_path} \
--ddp_timeout 72000 \
--save_steps 5000 \
--dataloader_num_workers 1 \
Expand Down