Skip to content

Commit b38dea7

Browse files
MLP Tutorials v1.0 (#85)
1 parent 5e90b59 commit b38dea7

File tree

6 files changed

+771
-2
lines changed

6 files changed

+771
-2
lines changed

docs/guides/mlp_tutorials/index.md

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[](){#ref-guides-mlp-tutorials}
2+
# MLP Tutorials
3+
4+
These tutorials solve simple MLP tasks using the [Container Engine][ref-container-engine] on the ML Platform.
5+
6+
1. [LLM Inference][ref-mlp-llm-inference-tutorial]
7+
2. [LLM Finetuning][ref-mlp-llm-finetuning-tutorial]
8+
3. [Nanotron Training][ref-mlp-llm-nanotron-tutorial]
9+
10+
11+
+195
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
[](){#ref-mlp-llm-finetuning-tutorial}
2+
3+
# LLM Finetuning Tutorial
4+
5+
This tutorial will take the model from the [LLM Inference][ref-mlp-llm-inference-tutorial] tutorial and show you how to perform finetuning.
6+
This means that we take the model and train it on some new custom data to change its behavior.
7+
8+
To complete the tutorial, we set up some extra libraries that will help us to update the state of the machine learning model.
9+
We also write a script that will allow us to unlock more of the performance offered by the cluster, by running our fine-tuning task on two or more nodes.
10+
11+
### Prerequisites
12+
13+
This tutorial assumes you've already successfully completed the [LLM Inference][ref-mlp-llm-inference-tutorial] tutorial.
14+
For fine-tuning Gemma, we will rely on the NGC PyTorch container and the libraries we've already installed in the Python environment used previously.
15+
16+
### Set up TRL
17+
18+
We will use HuggingFace TRL to fine-tune Gemma-7B on the [OpenAssistant dataset](https://huggingface.co/datasets/OpenAssistant/oasst_top1_2023-08-25).
19+
First, we need to update our Python environment with some extra libraries to support TRL.
20+
To do this, we can launch an interactive shell in the PyTorch container, just like we did in the previous tutorial.
21+
Then, we install `peft`:
22+
23+
```console
24+
$ cd $SCRATCH/gemma-inference
25+
$ srun --environment=gemma-pytorch --container-workdir=$PWD --pty bash
26+
$ source ./gemma-venv/bin/activate
27+
$ python -m pip install peft==0.11.1
28+
```
29+
30+
Next, we also need to clone and install the `trl` Git repository so that we have access to the fine-tuning scripts in it.
31+
For this purpose, we will install the package in editable mode in the virtual environment.
32+
This makes it available in python scripts independent of the current working directory and without creating a redundant copy of the files.
33+
34+
```console
35+
$ git clone https://github.com/huggingface/trl -b v0.7.11
36+
$ pip install -e ./trl # install in editable mode
37+
```
38+
39+
When this step is complete, you can exit the shell by typing `exit`.
40+
41+
### Finetune Gemma-7B
42+
43+
t this point, we can set up a fine-tuning script and start training Gemma-7B.
44+
Use your favorite text editor to create the file `fine-tune-gemma.sh` just outside the trl and gemma-venv directories:
45+
46+
```bash title="fine-tune-gemma.sh"
47+
#!/bin/bash
48+
49+
source ./gemma-venv/bin/activate
50+
51+
set -x
52+
53+
export HF_HOME=$SCRATCH/huggingface
54+
export TRANSFORMERS_VERBOSITY=info
55+
56+
ACCEL_PROCS=$(( $SLURM_NNODES * $SLURM_GPUS_PER_NODE ))
57+
58+
MAIN_ADDR=$(echo "${SLURM_NODELIST}" | sed 's/[],].*//g; s/\[//g')
59+
MAIN_PORT=12802
60+
61+
accelerate launch --config_file trl/examples/accelerate_configs/multi_gpu.yaml \
62+
--num_machines=$SLURM_NNODES --num_processes=$ACCEL_PROCS \
63+
--machine_rank $SLURM_PROCID \
64+
--main_process_ip $MAIN_ADDR --main_process_port $MAIN_PORT \
65+
trl/examples/scripts/sft.py \
66+
--model_name google/gemma-7b \
67+
--dataset_name OpenAssistant/oasst_top1_2023-08-25 \
68+
--per_device_train_batch_size 2 \
69+
--gradient_accumulation_steps 1 \
70+
--learning_rate 2e-4 \
71+
--save_steps 200 \
72+
--max_steps 400 \
73+
--use_peft \
74+
--lora_r 16 --lora_alpha 32 \
75+
--lora_target_modules q_proj k_proj v_proj o_proj \
76+
--output_dir gemma-finetuned-openassistant
77+
```
78+
79+
This script has quite a bit more content to unpack.
80+
We use HuggingFace accelerate to launch the fine-tuning process, so we need to make sure that accelerate understands which hardware is available and where.
81+
Setting this up will be useful in the long run because it means we can tell SLURM how much hardware to reserve, and this script will setup all the details for us.
82+
83+
The cluster has four GH200 chips per compute node.
84+
We can make them accessible to scripts run through srun/sbatch via the option `--gpus-per-node=4`.
85+
Then, we calculate how many processes accelerate should launch.
86+
We want to map each GPU to a separate process, this should be four processes per node.
87+
We multiply this by the number of nodes to obtain the total number of processes.
88+
Next, we use some bash magic to extract the name of the head node from SLURM environment variables.
89+
Accelerate expects one main node and launches tasks on the other nodes from this main node.
90+
Having sourced our python environment at the top of the script, we can then launch Gemma fine-tuning.
91+
The first four lines of the launch line are used to configure accelerate.
92+
Everything after that configures the `trl/examples/scripts/sft.py` Python script, which we use to train Gemma.
93+
94+
Next, we also need to create a short SLURM batch script to launch our fine-tuning script:
95+
96+
```bash title="fine-tune-sft.sbatch"
97+
#!/bin/bash
98+
#SBATCH --job-name=gemma-finetune
99+
#SBATCH --time=00:30:00
100+
#SBATCH --ntasks-per-node=1
101+
#SBATCH --gpus-per-node=4
102+
#SBATCH --cpus-per-task=288
103+
#SBATCH --account=<ACCOUNT>
104+
105+
set -x
106+
107+
srun -ul --environment=gemma-pytorch --container-workdir=$PWD bash fine-tune-gemma.sh
108+
```
109+
110+
We set a few Slurm parameters like we already did in the previous tutorial.
111+
Note that we leave the number of nodes unspecified.
112+
This way, we can decide the number of nodes we want to use when we launch the batch job using Slurm.
113+
114+
Now that we've setup a fine-tuning script and a Slurm batch script, we can launch our fine-tuning job.
115+
We'll start out by launching it on two nodes.
116+
It should take about 10-15 minutes to fine-tune Gemma:
117+
118+
```console
119+
$ sbatch --nodes=1 fine-tune-sft.sbatch
120+
```
121+
122+
### Compare finetuned Gemma against default Gemma
123+
124+
We can reuse our python script from the first tutorial to do inference on the Gemma model that we just fine-tuned.
125+
Let's try out a different prompt in `gemma-inference.py`:
126+
127+
```python
128+
input_text = "What are the 5 tallest mountains in the Swiss Alps?"
129+
```
130+
131+
We can run inference using our batch script from the previous tutorial:
132+
133+
```console
134+
$ sbatch ./gemma-inference.sbatch
135+
```
136+
137+
Inspecting the output should yield something like this:
138+
139+
```
140+
<bos>What are the 5 tallest mountains in the Swiss Alps?
141+
142+
The Swiss Alps are home to some of the tallest mountains in the world. Here are
143+
the 5 tallest mountains in the Swiss Alps:
144+
145+
1. Mont Blanc (4,808 meters)
146+
2. Matterhorn (4,411 meters)
147+
3. Dom (4,161 meters)
148+
4. Jungfrau (4,158 meters)
149+
5. Mont Rose (4,117 meters)<eos>
150+
```
151+
152+
Next, we can update the model line in our Python inference script to use the model that we just fine-tuned:
153+
154+
```python
155+
model = AutoModelForCausalLM.from_pretrained("gemma-finetuned-openassistant/checkpoint-400", device_map="auto")
156+
```
157+
158+
If we re-run inference, the output will be a bit more detailed and explanatory, similar to output we might expect from a helpful chatbot. One example looks like this:
159+
160+
```
161+
<bos>What are the 5 tallest mountains in the Swiss Alps?
162+
163+
The Swiss Alps are home to some of the tallest mountains in Europe, and they are a popular destination for mountai
164+
neers and hikers. Here are the five tallest mountains in the Swiss Alps:
165+
166+
1. Mont Blanc (4,808 m/15,774 ft): Mont Blanc is the highest mountain in the Alps and the highest mountain in Euro
167+
pe outside of Russia. It is located on the border between France and Italy, and it is a popular destination for mo
168+
untaineers and hikers.
169+
170+
2. Dufourspitze (4,634 m/15,203 ft): Dufourspitze is the highest mountain in Switzerland and the second-highest mo
171+
untain in the Alps. It is located in the Valais canton of Switzerland, and it is a popular destination for mountai
172+
neers and hikers.
173+
174+
3. Liskamm (4,527 m/14,855 ft): Liskamm is a mountain in the Bernese Alps of Switzerland. It is located in the Ber
175+
n canton of Switzerland, and it is a popular destination for mountaineers and hikers.
176+
177+
4. Weisshorn (4,506 m/14,783 ft): Weisshorn is a mountain in the Pennine Alps of Switzerland. It is located in the
178+
Valais canton of Switzerland, and it is a popular destination for mountaineers and hikers.
179+
180+
5. Matterhorn (4,478 m/14,690 ft): Matterhorn is a mountain in the Pennine Alps of Switzerland. It is located in the Valais canton of Switzerland, and it is a popular destination for mountaineers and hikers.
181+
182+
These mountains are all located in the Swiss Alps, and they are a popular destination for mountaineers and hikers. If you are planning a trip to the Swiss Alps, be sure to check out these mountains and plan your itinerary accordingly.
183+
```
184+
185+
Your output may look different after fine-tuning, but in general you will see that the fine-tuned model generates more verbose output.
186+
Double-checking the output reveals that the list of mountains produced by Gemma is not actually correct.
187+
These are the 5 tallest Swiss peaks according to Wikipedia:
188+
189+
1. Dufourspitze 4,634m
190+
2. Nordend 4,609m
191+
3. Zumsteinspitz 4,563m
192+
4. Signalkuppe 4,554m
193+
5. Dom 4,545m
194+
195+
This is an important reminder that machine-learning models like Gemma need extra checks to confirm any generated outputs.

0 commit comments

Comments
 (0)