Skip to content

Commit 82bb008

Browse files
committed
3 READMEs update; fine-tuning requirements update with vllm etc
1 parent 6501cf4 commit 82bb008

4 files changed

Lines changed: 117 additions & 56 deletions

File tree

end-to-end-use-cases/coding/text2sql/README.md

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
11
# Improving Llama Text2SQL performance with CoT Fine-tuning
22

3-
This recipe is step by step guide to improve Llama performance on Text2SQL measured with the popular [BIRD](https://bird-bench.github.io) benchmark. We generate synthetic Chain of Thought(CoT) dataset and fine-tune Llama models on it.
3+
This recipe is step by step guide to improve Llama performance on Text2SQL measured with the popular [BIRD](https://bird-bench.github.io) benchmark. We generate a synthetic Chain of Thought(CoT) dataset and fine-tune Llama models on it.
44

5-
Results: [graph_placeholder]
5+
Results:
6+
|-----------------------------|-------------------------------|
7+
| baseline | 39.47% |
8+
| CoT, PEFT | 43.35% |
9+
| CoT, FFT | 42.44% (3 epochs) |
10+
| CoT, FFT | 43.87% (10 epochs) |
611

7-
We followed following steps:
12+
The complete steps are:
813

9-
1. Pre-processing the BIRD TRAIN datset by converting SQL statements into conversation format
14+
1. Pre-processing the BIRD TRAIN datset by converting SQL statements into the conversation format.
1015

11-
2. We use the conversations from step 1, add CoT to these existing conversations using Llama-3.3-70B
16+
2. We use the conversations from step 1, add CoT to these existing conversations using Llama-3.3-70B.
1217

13-
3. Fine-tuning Llama-3.1-8B on the dataset from step 2
18+
3. Fine-tuning Llama-3.1-8B on the dataset from step 2.
1419

15-
4. We provide scripts to simplify running the [BIRD](https://bird-bench.github.io) benchmark on the fine-tuned models and compare it with out of the model.
20+
4. We provide scripts to simplify running the [BIRD](https://bird-bench.github.io) eval benchmark on the fine-tuned models and compare it with out of the model.
1621

17-
## Structure:
22+
## Folder Structure
1823

1924
- quickstart folder: contains a notebook to ask Llama 3.3 to convert natural language queries into SQL queries.
2025
- data folder: contains scripts to download the BIRD TRAIN and DEV datasets;

end-to-end-use-cases/coding/text2sql/eval/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ Below are the results of the Llama models we have evaluated on the BIRD DEV data
1717

1818
## Quick Start with Llama Models via Llama API
1919

20-
First, run the commands below to create a new Conda environment and install all the required packages for Text2SQL evaluation and fine-tuning:
20+
Follow the steps below to evaluate Llama 3 & 4 models on Text2SQL using the BIRD benchmark:
21+
22+
1. Run the commands below to create a new Conda environment and install all the required packages for Text2SQL evaluation:
2123

2224
```
2325
conda create -n llama-text2sql python=3.10
@@ -28,18 +30,16 @@ cd llama-cookbook/end-to-end-use-cases/coding/text2sql/eval
2830
pip install -r requirements.txt
2931
```
3032

31-
Then, follow the steps below to evaluate Llama 3 & 4 models on Text2SQL using the BIRD benchmark:
32-
33-
1. Get the DEV dataset:
33+
2. Get the DEV dataset:
3434
```
3535
cd ../data
3636
sh download_dev_unzip.sh
3737
cd ../eval
3838
```
3939

40-
2. Open `llama_eval.sh` and set `YOUR_API_KEY` to your [Llama API](https://llama.developer.meta.com/) key then uncomment a line that starts with `model=` to specify the Llama model to use for the text2sql eval.
40+
3. Open `llama_eval.sh` and set `YOUR_API_KEY` to your [Llama API](https://llama.developer.meta.com/) key then uncomment a line that starts with `model=` to specify the Llama model to use for the text2sql eval.
4141

42-
3. Run the evaluation script `sh llama_eval.sh`, which will use the BIRD DEV dataset (1534 examples in total) with external knowledge turned on to run the Llama model on each text question and compare the generated SQL with the gold SQL.
42+
4. Run the evaluation script `sh llama_eval.sh`, which will use the BIRD DEV dataset (1534 examples in total) with external knowledge turned on to run the Llama model on each text question and compare the generated SQL with the gold SQL.
4343

4444
If your API key or model name is incorrect, the script will exit with an authentication or model not supported error.
4545

end-to-end-use-cases/coding/text2sql/fine-tuning/README.md

Lines changed: 83 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This folder contains scripts to:
44

55
* generate a dataset from the BIRD TRAIN set (with no CoT info) for supervised fine-tuning (SFT);
66
* generate a dataset from the BIRD TRAIN set (with CoT info by Llama 3.3 70B) for SFT;
7-
* SFT the Llama 3.1 8B model with the generated datasets with different fine-tuning combinations: with or without CoT, using quantization or not, full fine-tuning (FFT) or parameter-efficient fine-tuning (PEFT).
7+
* SFT the Llama 3.1 8B model with the generated datasets with different fine-tuning combinations: with or without CoT, using quantization or not, full fine-tuning (FFT) or parameter-efficient fine-tuning (PEFT).
88

99
**Note:** CoT stands for Chain of Thought and we will use "CoT" and "reasoning" interchangeably here, although generally, reasoning encompasses a broader concept than CoT.
1010

@@ -22,15 +22,92 @@ The eval results of SFT Llama 3.1 8B with different options (epochs is 3, with a
2222

2323
Using Quantization+PEFT on CoT dataset only dropped the accuracy from 43.35% to 42.89%.
2424

25-
## Creating dataset
25+
## Quick Start with Fine-tuning Llama 3.1 8B
2626

27-
We use the BIRD TRAIN dataset to prepare for supervised fine-tuning with reasoning info in the dataset. The goal is to see if we can improve the accuracy of the fine-tuned model by adding the reasoning info in the dataset.
27+
1. If you have already run the eval folder's Quick Start Step 1's commands [here](../eval/README.md#quick-start-with-llama-models-via-llama-api) to "create a new Conda environment and install all the required packages for Text2SQL evaluation", just run:
28+
29+
```
30+
cd llama-cookbook/end-to-end-use-cases/coding/text2sql/fine-tuning
31+
pip install -r requirements.txt
32+
```
33+
34+
Otherwise, run the commands below to create a new Conda environment and install all the required packages for Text2SQL evaluation and fine-tuning:
35+
36+
```
37+
conda create -n llama-text2sql python=3.10
38+
conda activate llama-text2sql
39+
git clone https://github.com/meta-llama/llama-cookbook
40+
git checkout text2sql # to be removed after the PR merge
41+
cd llama-cookbook/end-to-end-use-cases/coding/text2sql/fine-tuning
42+
pip install -r requirements.txt
43+
```
44+
45+
2. Get the TRAIN dataset:
46+
47+
```
48+
cd ../data
49+
sh download_train_unzip.sh
50+
cd ../fine-tuning
51+
```
52+
53+
3. Create a CoT reasoning dataset from the TRAIN dataset:
54+
55+
```
56+
python create_reasoning_dataset.py --input_json ../data/train/train.json --db_root_path ../data/train/train_databases
57+
```
58+
59+
See the section "About Creating the CoT Dataset" below for more details.
60+
61+
4. Run one of the commands below to fine-tune the Llama 3.1 8B model with the generated dataset (about 50-70GB GPU memory required):
62+
63+
```
64+
python trl_sft.py --quantized false --peft false --cot true
65+
python trl_sft.py --quantized false --peft true --cot true
66+
python trl_sft.py --quantized true --peft true --cot true
67+
```
68+
69+
See the section "About fine-tuning" below for more details.
70+
71+
## Evaluating the fine-tuned model
72+
73+
1. Set the `model` value in `llama_eval.sh` to be one of the fine-tuned model folders above, e.g.
74+
75+
```
76+
YOUR_API_KEY='finetuned'
77+
model='fine_tuning/llama31-8b-text2sql-fft-nonquantized-cot'
78+
```
79+
80+
2. Start the vllm server by running
81+
```
82+
vllm serve fine_tuning/llama31-8b-text2sql-fft-nonquantized-cot --tensor-parallel-size 1 --max-num-batched-tokens 8192 --max-num-seqs 64
83+
```
84+
If you have multiple GPUs you can run something like
85+
86+
```
87+
CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 vllm serve fine_tuning/llama31-8b-text2sql-fft-nonquantized-cot --tensor-parallel-size 8 --max-num-batched-tokens 8192 --max-num-seqs 64
88+
```
89+
90+
to speed up the eval.
2891

29-
### Creating a reasoning dataset from the TRAIN dataset
92+
3. If you haven't downloaded the DEV dataset, download it and unzip it first:
93+
94+
```
95+
cd ../data
96+
sh download_dev_unzip.sh
97+
cd ../eval
98+
```
99+
100+
Then run `sh llama_eval.sh`.
101+
102+
**Note:** If your fine-tuned model is PEFT based, you may need to run `python merge_peft.py` after modifying its `peft_model_path` and `output_dir` and set the merged folder path after `vllm serve`.
103+
104+
## About Creating the CoT Dataset
105+
106+
We use the BIRD TRAIN dataset to prepare for supervised fine-tuning with reasoning info in the dataset. The goal is to see if we can improve the accuracy of the fine-tuned model by adding the reasoning info in the dataset.
30107

31108
The script `create_reasoning_dataset.py` is used to create a reasoning dataset from the TRAIN dataset by asking Llama 3.3 70B to generate the reasoning for each text question and its corresponding gold SQL. The intent is to use the reasoning dataset to fine-tune the Llama model to improve the accuracy of the generated SQL.
32109

33-
To run the script, use the following commands:
110+
To run the script, use the following command:
34111
```
35112
python create_reasoning_dataset.py --input_json ../data/train/train.json --db_root_path ../data/train/train_databases
36113
```
@@ -71,7 +148,7 @@ Let me think through this step by step:\n\n1. First, I need to consider...\n2. T
71148
"""
72149
```
73150

74-
### Running fine-tuning
151+
## About fine-tuning
75152

76153
Run one of the commands below:
77154

@@ -91,26 +168,3 @@ llama31-8b-text2sql-peft-quantized-cot
91168

92169
The train loss chart should look like this:
93170
![](train_loss_cot.png)
94-
95-
### Evaluating the fine-tuned model
96-
97-
1. Set the `model` value in `llama_eval.sh` to be one of the fine-tuned model folders above, e.g.
98-
99-
```
100-
YOUR_API_KEY='finetuned'
101-
model='fine_tuning/llama31-8b-text2sql-fft-nonquantized-cot'
102-
```
103-
104-
2. Start the vllm server by running
105-
```
106-
vllm serve fine_tuning/llama31-8b-text2sql-fft-nonquantized-cot --tensor-parallel-size 1 --max-num-batched-tokens 8192 --max-num-seqs 64
107-
```
108-
If you have multiple GPUs you can run something like
109-
```
110-
CUDA_VISIBLE_DEVICES=0,1,2,3,4,5,6,7 vllm serve fine_tuning/llama31-8b-text2sql-fft-nonquantized-cot --tensor-parallel-size 8 --max-num-batched-tokens 8192 --max-num-seqs 64
111-
```
112-
to speed up the eval.
113-
114-
3. Run `sh llama_eval.sh`.
115-
116-
**Note:** If your fine-tuned model is PEFT based, you may need to run `python merge_peft.py` after modifying its `peft_model_path` and `output_dir` and set the merged folder path after `vllm serve`.
Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
llama_api_client==0.1.1
1+
llama_api_client==0.1.2
2+
func_timeout==4.3.5
3+
tqdm==4.67.1
4+
vllm==0.9.2
5+
openai==1.90.0
26
langchain-together==0.3.0
37
sqlparse==0.5.3
4-
torch==2.4.1
58
tensorboard==2.19.0
6-
liger-kernel==0.4.2
9+
liger_kernel==0.6.1
710
setuptools==78.1.1
8-
deepspeed==0.15.4
9-
transformers==4.46.3
10-
datasets==3.6.0
11-
accelerate==1.1.1
12-
bitsandbytes==0.44.1
13-
trl==0.12.1
14-
peft==0.13.2
15-
lighteval==0.6.2
16-
hf-transfer==0.1.8
17-
func_timeout==4.3.5
11+
deepspeed==0.17.3
12+
transformers==4.54.0
13+
datasets==4.0.0
14+
accelerate==1.9.0
15+
bitsandbytes==0.46.1
16+
trl==0.19.1
17+
peft==0.16.0
18+
lighteval==0.10.0
19+
hf_transfer==0.1.9

0 commit comments

Comments
 (0)